注意
vue3 删除了 $on/$once/$off
API
一般使用
缺点不利于维护 代码冗余
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| data() { return { timer: null } }, mouted() { this.timer = (() => { }, 1000) }, beforeDestroy() { clearInterval(this.timer); this.timer = null; }
|
$once 一次性监听器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| export default { data () { return { DateTime: '' } }, created () { this.timeInterval() }, methods: { timeInterval () { const timer = setInterval(() => { this.DateTime = new Date() }, 1000) this.$once('hook:beforeDestroy', () => { clearInterval(timer) }) } } }
|