【发布时间】:2022-01-12 12:43:02
【问题描述】:
我的项目中有这样的代码:
<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>
当我设置num.value = 1 时,我的watcher 不起作用。我怎样才能解决这个问题?
但是当我使用setTimeout 运行时,它就可以了
演示项目 here
【问题讨论】:
标签: javascript vue.js vuejs3 watch vue-sfc