vue-过滤器

vue-过滤器

过滤器: 数据格式化, 比如,后端返回我们的数据格式是一个时间戳,

在页面中使用的时候需要变成真正的时间格式 2018-12-26

过滤器使用
<!-- 在双花括号中 -->
{{ time | filterTime }}

<!-- 在 `v-bind` 中 -->
<div v-bind:id="time | filterTime"></div>
全局过滤器
 //全局过滤器  内部必须要写return,默认符号为 - ,可以根据自己的需要传值
 Vue.filter("filterTime", function (val,option='-') {
     let date = new Date(val);
     return date.getFullYear() + option + (date.getMonth() + 1) + option + date.getDate();
    })
//结果:2020-8-13

//当传入参数时  filterTime(/)
//结果为:2020/8/13
局部过滤器
new Vue({
        el:"#app",
        data:{
          money:100
        },
        filters:{ //局部的过滤器
          filterMoney(money,option='$'){
            return option+money
          }
        }
      })
结果:$100
posted @ 2020-08-13 20:49  Cupid05  阅读(65)  评论(0编辑  收藏  举报