如何在vue上全局使用过滤器filter

夏沐沐

关注

阅读 42

2022-03-30

如何在vue上全局使用过滤器filter

在src下新建文件夹utils,创建filter.js文件

import Vue from 'vue'

Vue.filter("publishdate",function publishdate(value){
  let json_date = new Date(value).toJSON();
  let s = new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
  return s;
})

可直接在vue文件里使用过滤器,不需要import

<div class="div3">
   <span class="span2">创建时间:{{this.publishdate | publishdate}}</span>
</div>

this.publishdate的所有数据都会经过过滤器publishdate,再返回。

精彩评论(0)

0 0 举报