頁面中的過濾器:
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>