我創建了一個倒數計時器,它可以完美地減少模板中的數字,但現在我需要它在達到 0 后啟動方法中宣告的函式。我試圖檢查方法中是否滿足條件但它沒有啟動達到0時的任何東西。
這是我的 index.vue
<template>
<div>
{{ timerCount }}
</div>
</template>
<script>
export default {
data(){
return{
timerCount: 60
}
},
watch: {
timerEnabled(value) {
if (value) {
setTimeout(() => {
this.timerCount = this.timerCount - 1;
}, 1000);
}
},
timerCount: {
handler(value) {
if (value > 0 && this.timerEnabled) {
setTimeout(() => {
this.timerCount = this.timerCount - 1;
}, 1000);
}
},
immediate: true
},
},
methods:{
launchThis() {
// I need this function to be launched if timerCount reaches 0
}
}
}
</script>
任何使其作業的指導將不勝感激。
uj5u.com熱心網友回復:
你可以使用這樣的東西
<script>
export default {
watch: {
timerCount: {
handler(value) {
if (value > 0 && this.timerEnabled) {
setTimeout(() => {
this.timerCount = this.timerCount - 1;
}, 1000);
} else {
this.launchThis() // run your function here
}
},
immediate: true
},
},
}
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/484193.html
標籤:javascript Vue.js Vuejs2 nuxt.js
