vue内JS代码中使用filter方案总结
1.需求背景
- 最近在vue项目中使用到了filter,常见的情况是在vue模版中使用,也有一些情况是在JS代码中使用,下面对其进行总结:
解决方案
-
// 1.全局新增一个filter(Vue单独引入),如: Vue.filter('dictionary', function(value, code) { if (code) { return localStorage.get('DICT_DATA')[code]; } return '' }) // 2.在模版中使用: <span>{{value|dictionary('documents_type')}}</span> // 3.在JS中使用,方法1: this.$options.filters['dictionary'](value,'documents_type') // 4.在JS中使用,方法2: Vue.filter('dictionary')(value,'documents_type')