首页path:'/'
倒计时结束后要清除计时器,防止内存泄漏:
if (this.count === 0) {
clearInterval(this.timer);
}
<!-- ErrorJump.vue -->
<template>
<h2>Error:找不到页面!</h2>
<h4>{{ count }}S后<RouterLink to="/">跳回首页</RouterLink></h4>
</template>
<script>
export default {
data() {
return {
count: 0,
timer: null,
};
},
mounted() {
this.count = 3;
this.timer = setInterval(() => {
this.count--;
if (this.count === 0) {
clearInterval(this.timer);
this.$router.push("/");
}
}, 1000);
},
};
</script>