vue3 倒计时3秒后返回首页
效果代码实现<template>{{ count }}秒后返回网站首页 <br></template><script lang="ts">import router from "/@/router";import {onMounted, reactive, ref} from "vue";export default {setup(): any {o
·
效果
代码实现
<template>
{{ count }}秒后返回网站首页 <br>
</template>
<script lang="ts">
import router from "/@/router";
import {onMounted, reactive, ref} from "vue";
export default {
setup(): any {
onMounted(() => {
countdown() //页面一加载成功就执行
})
let timer = null
let count: number = ref(10)
//倒计时
function countdown() {
timer = setInterval(() => {
count.value = count.value-1
if (count.value <= 0) {
clearInterval(timer);
timer = null;
//跳转的页面写在此处
router.push({path: "/"}); // 强制切换当前路由 path
}
}, 1000)
}
return {
count
}
}
}
</script>
更多推荐
已为社区贡献15条内容
所有评论(0)