我的專案中有這樣的代碼:
<script setup>
import { ref, watch } from 'vue'
const num = ref(null)
// Some condition
if(true) {
// Doesn't works. Why?
num.value = 1
// Works
//setTimeout(() => {
// num.value = 2
//})
}
// Simple watcher
watch(num, (newVal, oldVal) => {
console.log("Num changed to: ", newVal)
})
</script>
我watcher當我設定不起作用num.value = 1。我該如何解決這個問題?但是當我運行setTimeout它的作業
演示專案 在這里
uj5u.com熱心網友回復:
在將其設定為 1 后添加觀察者,因此它沒有機會捕捉到它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/375415.html
標籤:javascript Vue.js Vuejs3 手表 vue-sfc
下一篇:如何為每個td獲取不同的名稱?
