Loading

vue项目中定义全局filter

参考博客:

  https://www.cnblogs.com/zhaoxiaoying/p/10723063.html

 

1、在format.js中定义全局filter函数

export  default {
    formatDate(value) {
        let date = new Date(value.replace(/-/g, "/"))
        let year =  date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let hours = date.getHours();
        if(hours<10){
            hours = "0"+ hours;
        }
        let minutes = date.getMinutes();
        if(minutes<10){
            minutes = "0"+ minutes;
        }
        return year +"年" + month + "月" + day + "日" + " " + hours + ":" + minutes;
    },

    //转化直播状态
    formatLiveStatus(status) {
        let temp
        switch(status) {
            case 1:
                temp = "直播中";
                break;
            case 2:
                temp = "已结束";
                break;
            default:
                temp = "待开播";
        }
        return temp;
    },

    //16999
    tranNumber(num, point) {
        let numStr = num.toString()
        if(numStr.length < 5){
            return numStr
        }
        else if (numStr.length < 6) {
            return numStr.substring(0, numStr.length - 3) + "." + numStr.substring(numStr.length - 3, numStr.length - 2) + "k"
        } else {
            return numStr.substring(0, numStr.length - 4) + "." + numStr.substring(numStr.length - 4, numStr.length - 3) + "w"
        }
    }
}

 

2、在main.js中引入

import filters from './util/format'

//全局filter
Object.keys(filters).forEach(k => {
  Vue.filter(k, filters[k])
})

 

3、在各个组件中直接使用即可

 

posted @ 2021-12-20 19:19  青岑  阅读(334)  评论(0编辑  收藏  举报