vue定义全局过滤器

新建承载过滤器的文件filters.js

var fileToUrl = function (val) {
  
    return val;
}

var timeFormat=function(time,fmStr){
    fmStr=fmStr||"yyyy-mm-dd hh:MM:ss"
    const weekCN = '一二三四五六日';
    const weekEN = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
    console.log(typeof time)
    if(typeof time=="number"){
        time=new Date(time)
	}
	if(!time){
		return "";
	}
	let year = time.getFullYear();
	let month = time.getMonth() + 1;
	let day = time.getDate();
	let hours = time.getHours();
	let minutes = time.getMinutes();
	let seconds = time.getSeconds();
	let milliSeconds = time.getMilliseconds();
	let week = time.getDay();
    console.log(fmStr)
	month = month >= 10 ? month : ('0' + month);
	day = day >= 10 ? day : ('0' + day);
	hours = hours >= 10 ? hours : ('0' + hours);
	minutes = minutes >= 10 ? minutes : ('0' + minutes);
	seconds = seconds >= 10 ? seconds : ('0' + seconds);

	if (fmStr.indexOf('yyyy') !== -1) {
		fmStr = fmStr.replace('yyyy', year);
	} else {
		fmStr = fmStr.replace('yy', (year + '').slice(2));
    }
   
	fmStr = fmStr.replace('mm', month);
	fmStr = fmStr.replace('dd', day);
	fmStr = fmStr.replace('hh', hours);
	fmStr = fmStr.replace('MM', minutes);
	fmStr = fmStr.replace('ss', seconds);
	fmStr = fmStr.replace('SSS', milliSeconds);
	fmStr = fmStr.replace('W', weekCN[week - 1]);
	fmStr = fmStr.replace('ww', weekEN[week - 1]);
	fmStr = fmStr.replace('w', week);

	return fmStr;
}

export {
    fileToUrl,
	timeFormat
}

min.js引入

import * as filters from './plugins/filters.js'
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

vue页面中可直接使用

<div class="create-time">{{contract.createDate | timeFormat("yyyy-mm-dd") }}</div>

 

发布了5 篇原创文章 · 获赞 1 · 访问量 71
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览