1 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊
只需存儲(chǔ)now在組件數(shù)據(jù)中并創(chuàng)建計(jì)算屬性,該屬性將計(jì)算存儲(chǔ)now和enfant.end
更新now在setInterval...
請(qǐng)參閱這個(gè)簡(jiǎn)單的時(shí)鐘示例:
new Vue({
el: '#app',
data: {
now: new Date(),
timer: null
},
computed: {
time: function() {
return this.now.toLocaleTimeString();
}
},
mounted: function() {
let self = this;
this.timer = setInterval(function() {
self.now = new Date()
}, 1000)
},
beforeDestroy: function() {
clearInterval(this.timer)
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div>{{ time }}</div>
</div>
添加回答
舉報(bào)