vue3 實(shí)現(xiàn)監(jiān)聽store里state狀態(tài)變化
import { watch } from "vue";
watch(
() => store.state.currentDevice,
(newVal, oldVal) => {
// to do
reload();
}
);
需要注意:不能直接監(jiān)聽對象的屬性值,需要寫成getter函數(shù)。
總結(jié):watch
的第一個參數(shù)可以是不同形式的數(shù)據(jù)源,它可以是一個ref(包括計算屬性),一個響應(yīng)式對象,一個getter函數(shù),或多個數(shù)據(jù)源組成的數(shù)組。
不能直接監(jiān)聽響應(yīng)式對象的屬性:
const obj = reactive({ count: 0 })
// 錯誤,因?yàn)?watch() 得到的參數(shù)是一個 number
watch(obj.count, (count) => {
console.log(`count is: ${count}`)
})
這里需要寫成返回對象屬性的getter的函數(shù)文章來源:http://www.zghlxwxcb.cn/news/detail-580365.html
watch(
()=>obj.count,
(count)=>{
// todo
console.log(`count is: ${count}`)
}
)
參考文檔:vue3-偵聽器文章來源地址http://www.zghlxwxcb.cn/news/detail-580365.html
到了這里,關(guān)于vue3 實(shí)現(xiàn)監(jiān)聽store里state狀態(tài)變化的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!