Vue project-simple implementation of global registration filter + useful filter function

Create a new filter.js file, which defines the required filter function, the format is as follows

/**
 * 格式化日期
 * @param {string} time 时间戳
 * @param {string} type 分隔符 需要手动传入
 * 使用方法 {
    
    {1946777545617 | formatDate('/')}}
 */
export const formatDate = (time, type) => {
    
    
  if (time) {
    
    
    var date = new Date();
    date.setTime(time);
    var year = date.getFullYear();
    var month =
      date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1) * 1
        : date.getMonth() + 1;
    var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    if (type) {
    
    
      return year + type + month + type + day;
    } else {
    
    
      return year + "年" + month + "月" + day + "日";
    }
  }
};

Use the global filter registration method in the main.js file, and the data in the project is followed by the | pipe symbol.

//全局过滤器注册
import * as filters from './config/filters/filters'

Object.keys(filters).forEach(key => {
    
    
  Vue.filter(key,filters[key])
})

import * as xxx from ""Add all the imported data to the xxx object
Object.keysTake out the key value in the object and add it to the array

Guess you like

Origin blog.csdn.net/weixin_51198863/article/details/113251559