过滤器主要用于当接口返回的数据和前台需要展示的数据不一样时,进行数据转换。下面例子实现 0 ---》 一 的转换
方法:
filters: text(val) { var text = ""; switch (val - 0) { case 0: text = "一"; break; case 1: text = "二"; break; case 2: text = "三"; break; case 3: text = "四"; break; case 4: text = "五"; break; case 5: text = "六"; break; case 6: text = "七"; break; case 7: text = "八"; break; case 8: text = "九"; break; case 9: text = "十"; break; } return text; }, },
模板中的写法:
<p>{{ index | text }}</p>
在methods方法中的使用:
this.$options.filters['text'](i)