Vueプロジェクト-グローバル登録フィルターの簡単な実装+便利なフィルター機能

必要なフィルター関数を定義する新しいfilter.jsファイルを作成します。形式は次のとおりです。

/**
 * 格式化日期
 * @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 + "日";
    }
  }
};

main.jsファイルでグローバルフィルター登録メソッドを使用すると、プロジェクトのデータの後に|パイプ記号が続きます。

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

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

import * as xxx from ""インポートされたすべてのデータをxxxオブジェクトに
Object.keys追加しますオブジェクトのキー値取り出して配列に追加します

おすすめ

転載: blog.csdn.net/weixin_51198863/article/details/113251559