vue中使用过滤器this为undefined解决方案

在 Vue 应用中遇到全局封装的时间日期格式化方法在组件内无法正常工作的问题,因为 this 为 undefined。解决方案是将格式化方法移至 methods 中,并在模板中直接调用。同时展示了如何在 Vue.prototype 中注入全局方法,以便在各个组件中方便地使用日期和时间格式化。
摘要由CSDN通过智能技术生成

业务场景:

全局封装了一个format的时间日期格式化方法,在当前.vue页面通过filters:{}或者Vue.filter定义过滤器,this为undefined

 


解决方案:

可以在methods:{ ... }中定义方法,替代filter

      <el-table-column prop="syncRowVersion" :label="l('CreationTime')">
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.syncRowVersion) }}</span>
        </template>
      </el-table-column>
    formatDate(value) {
      return this.$ux.format.dateTime(value);
    }

 

 


补充:

上面的 this.$ux.format.dateTime(value),是在全局定义的foamat方法并在Vue.prototype中注入

/**
 *
 * 公用的时间日期、价格等格式化方法
 */

import format from "number-format.js";
import moment from "moment";

export default {
  priceFormat: "#,##0.00",
  amountFormat: "#,##0.00",
  moneyFormat: "#,##0",
  costFormat: "#,##0.00",
  qtyFromat: "#,##0",
  currencyFormat: "#,##0.000000",
  weightFormat: "#,##0.0",
  volumeFormat: "#,##0.0",
  dateFormat: "YYYY-MM-DD",
  dateTimeFormat: "YYYY-MM-DD HH:mm:ss",
  elementDateFormat: "yyyy-MM-dd",
  elementDateTimeFormat: "yyyy-MM-dd HH:mm:ss",

  //通用数值格式,用于非price,amount,qty等特殊地方
  numberFormat0: "#,##0",
  numberFormat2: "#,##0.00",

  dateTimebySales(dt, format = "DD/MM/YYYY HH:mm") {
    if (!dt) return "";
    return moment(dt).format(format);
  },
  date(dt, format = "YYYY/MM/DD") {
    if (!dt) return "";
    return moment(dt).format(format);
  },
  dateTime(dt, format = "YYYY/MM/DD HH:mm:ss") {
    if (!dt) return "";
    return moment(dt).format(format);
  },
  price(v) {
    return format(v, this.priceFormat);
  },
  cost(v) {
    return format(v, this.costFormat);
  },

  amount(v) {
    return format(v, this.amountFormat);
  },
  qty(v) {
    return format(v, this.qtyFromat);
  },

  columnFormatter(type) {
    let thiz = this;
    switch (type) {
      case "date":
        return function (row, column, cellValue, index) {
          return thiz.date(cellValue);
        };
      case "dateTime":
        return function (row, column, cellValue, index) {
          return thiz.dateTime(cellValue);
        };
      case "price":
        return function (row, column, cellValue, index) {
          return thiz.price(cellValue);
        };
      case "amount":
        return function (row, column, cellValue, index) {
          return thiz.amount(cellValue);
        };
      case "qty":
        return function (row, column, cellValue, index) {
          return thiz.qty(cellValue);
        };
      case "cost":
        return function (row, column, cellValue, index) {
          return thiz.cost(cellValue);
        };
    }
  },
};

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Iam_楠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值