1.定义filter/filter.js文件:
function filterOne(n){ return n + 10; } function filterTwo(n){ return n + 5; } export{ filterOne, filterTwo }
2.在入口文件引入:
// 找 filter/filter.js import * as filters from './filter/filter.js' //遍历所有导出的过滤器并添加到全局过滤器 Object.keys(filters).forEach((key) => { Vue.filter(key, filters[key]); })
3.在组件中使用:
{{test | filterOne}}