页面中的过滤器:

filters: {
            formatDate(date){
                const ndate = new Date(date)
                const year = ndate.getFullYear()
                const month = ndate.getMonth().toString().padStart(2,0)
                const day = ndate.getDay().toString().padStart(2,0)
                return year + "-" + month + "-" + day
                
            }
        },

由于有多个页面都要使用该过滤器,我们不用在每个页面都添加如上代码,而是在main.js中定义一个全局过滤器:

import Vue from 'vue'
import App from './App'
import { myRequest } from './util/api.js'

Vue.prototype.$myRequest = myRequest
Vue.config.productionTip = false
Vue.filter("formatDate",(date)=>{
    const ndate = new Date(date)
    const year = ndate.getFullYear()
    const month = ndate.getMonth().toString().padStart(2,0)
    const day = ndate.getDay().toString().padStart(2,0)
    return year + "-" + month + "-" + day
})
App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()

使用过滤器:

<text>发表时间:{{item.add_time | formatDate}}</text>

 

posted on 2021-03-12 14:43  周文豪  阅读(947)  评论(0编辑  收藏  举报