【FAQ】Vue3监听Pinia中某一变量的变化
直接使用A例子是监听不到pinia的变化的!要像B例子写!
A错误写法
import mainStore form '@/store/index'
const useMain = mainStore()
// 假设pinia中有一个count变量(const count = ref(0))
watch(useMain.conut, (newVal, oldVal) => {
console.log(newVal, oldVal)
})
B正确写法
import mainStore form '@/store/index'
const useMain = mainStore()
// 假设pinia中有一个count变量(const count = ref(0))
watch(() => useMain.conut, (newVal, oldVal) => {
console.log(newVal, oldVal)
})