过滤器的定义与使用

定义过滤器:

过滤器作用与一些常见的文本格式化,过滤器可以用在两个地方:msg(插值)和v-bind表达式上

全局过滤器
Vue.filter(xx,function(data,con1,con2,...){.....})

Vue.filter(过滤器的名字,执行函数(固定参数,内容1,内容2...){ 处理代码 })

私有过滤器
var vm=new Vue({
    el:'xx',
    data:{xxx},
    filters:{
       xx:function(data,con1,con2...){
            .....
    }}
})

如果全局和私有过滤器的名字相同,则执行私有的

过滤器的使用:

<p>{{msg | addMsg(0,'+++'}}</P>
msg: 'A1B1C1D1'
Vue.filter('addMsg',function(data,con1,con2){
    return data.replace(/1/g,con1)+con2
    //使用正则,把msg中的1全局替换为con1,然后再在末尾加上con2,输出
})

最后的结果为:A0B0C0D0+++

猜你喜欢

转载自blog.csdn.net/Zyl_CN/article/details/82976950