«

Vue教程:如何在methods中使用全局过滤器

时间:2023-6-8 10:33     作者:六思逸     分类: Vue


今天写东西的时候想到一个问题,然后就去查阅了一下相关的文档,也是找到了解决方案.

问题:如何在Vue的methods中使用全局过滤器

解决方案

全局过滤器

Vue.filter("timeT", (d) => {
    const date = new Date(d);
    const year = date.getFullYear();
    const month = ("0" + (date.getMonth() + 1)).slice(-2);
    const day = ("0" + date.getDate()).slice(-2);
    const hour = ("0" + date.getHours()).slice(-2);
    const minute = ("0" + date.getMinutes()).slice(-2);
    const second = ("0" + date.getSeconds()).slice(-2);
    const formattedTime = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    return formattedTime;
});

methods中使用

注意:需要引入Vue

import Vue from "vue";

第一步:在methods中定义处理数据的方法

methods: {
  timeT(d) {
    let timeT = Vue.filter('timeT');
    return timeT(d);
  }
}

第二步:在需要使用数据的地方调用处理数据的方法

this.tableData.orderTime = res.data.data.map((v) => {
  return this.timeT(v.orderTime);
});

标签: Vue methods 过滤器

版权所有:六思逸
文章标题:Vue教程:如何在methods中使用全局过滤器
除非注明,文章均为 六思逸 原创,转载请注明作者和出处 六思逸

扫描二维码,在手机上阅读

推荐阅读: