在vue脚手架中创建全局过滤器,格式化日期

首先在src目录下创建  filter/index.js文件

exports.formateDate=(value)=>{
    var time = new Date(value * 1000);
    var year = time.getFullYear();
    var month = time.getMonth() + 1 > 9 ? (time.getMonth() + 1) : "0" + (time.getMonth() + 1)
    var day = time.getDate();
    var hour = time.getHours();
    var minutes = time.getMinutes();
    var seconds = time.getSeconds();
    return `${year}-${month}-${day} ${hour}:${minutes}:${seconds}`
}

在main.js中引入

import filters from "./filter/index.js"
Object.keys(filters).forEach(k => Vue.filter(k, filters[k]));

在组件中使用

<p><a href="#">{{item.userName}}</a>发表博客: <a href="#">{{item.blogName}}</a> &nbsp;&nbsp;
                <span>{{item.publishTime|formateDate}}</span><span class="glyphicon glyphicon-trash" @click.stop="del(item.blogId)" v-show="blogListState==2"></span></p>
                <p class="blog-txt" v-html="item.blogTxt"></p>
原文地址:https://www.cnblogs.com/shanchui/p/13811493.html