mainjs
import dayjs from 'dayjs'
Vue.prototype.$dayJs = dayjs
页面
<span>{{ Time }}</span>
computed: {
Time () {
return this.$dayJs().format('YYYY-MM-DD HH:mm:ss')
}
},
<span>{{ time }}</span>
data(){
return {
timer: null,
time: null,
}
},
mounted(){
this.time = this.$dayJs().format('YYYY-MM-DD HH:mm:ss')
this.timer = setInterval(() => {
this.time = this.$dayJs().format('YYYY-MM-DD HH:mm:ss')
}, 1000)
},
beforeDestroy () {
if (this.timer) {
clearInterval(this.timer)
}
}