Vue3監(jiān)聽(tīng)reactive對(duì)象中屬性變化的方法
在 Vue 3 中,如果你想監(jiān)聽(tīng) reactive 對(duì)象中的某個(gè)屬性發(fā)生的變化,你可以使用 watch 函數(shù)進(jìn)行監(jiān)聽(tīng)。watch 函數(shù)允許你觀察 reactive 對(duì)象的某個(gè)屬性或者整個(gè)對(duì)象,并在變化時(shí)執(zhí)行相應(yīng)的操作。
1. 監(jiān)聽(tīng) reactive 對(duì)象的某個(gè)屬性
如果你只想監(jiān)聽(tīng) reactive 對(duì)象的某個(gè)特定屬性,可以直接在 watch 中傳入該屬性。
import { reactive, watch } from 'vue';
const state = reactive({
name: 'John',
age: 30,
location: 'New York',
});
// 監(jiān)聽(tīng) name 屬性的變化
watch(() => state.name, (newValue, oldValue) => {
console.log(`Name changed from ${oldValue} to ${newValue}`);
});
2. 監(jiān)聽(tīng)整個(gè) reactive 對(duì)象并檢查是哪一個(gè)屬性發(fā)生了變化
如果你需要監(jiān)聽(tīng)整個(gè) reactive 對(duì)象,并確定到底是哪個(gè)屬性發(fā)生了變化,可以通過(guò)對(duì)整個(gè)對(duì)象進(jìn)行深度監(jiān)聽(tīng) (deep: true),然后手動(dòng)檢查哪個(gè)屬性發(fā)生了變化。
import { reactive, watch } from 'vue';
const state = reactive({
name: 'John',
age: 30,
location: 'New York',
});
// 監(jiān)聽(tīng)整個(gè)對(duì)象的變化
watch(
state,
(newValue, oldValue) => {
for (const key in newValue) {
if (newValue[key] !== oldValue[key]) {
console.log(`${key} changed from ${oldValue[key]} to ${newValue[key]}`);
}
}
},
{ deep: true } // 深度監(jiān)聽(tīng)
);
3. 監(jiān)聽(tīng)多個(gè)屬性
如果你需要監(jiān)聽(tīng)多個(gè)特定的屬性,你可以使用多個(gè) watch,或者通過(guò)組合使用 computed 進(jìn)行監(jiān)聽(tīng)。
import { reactive, watch } from 'vue';
const state = reactive({
name: 'John',
age: 30,
location: 'New York',
});
// 監(jiān)聽(tīng) name 和 age 屬性的變化
watch(
() => [state.name, state.age],
([newName, newAge], [oldName, oldAge]) => {
if (newName !== oldName) {
console.log(`Name changed from ${oldName} to ${newName}`);
}
if (newAge !== oldAge) {
console.log(`Age changed from ${oldAge} to ${newAge}`);
}
}
);
4. 使用 toRefs 進(jìn)行屬性監(jiān)聽(tīng)
你可以將 reactive 對(duì)象的屬性轉(zhuǎn)換為 ref,然后使用 watch 監(jiān)聽(tīng)這些 ref。
import { reactive, toRefs, watch } from 'vue';
const state = reactive({
name: 'John',
age: 30,
location: 'New York',
});
const { name, age } = toRefs(state);
// 監(jiān)聽(tīng) name 屬性的變化
watch(name, (newValue, oldValue) => {
console.log(`Name changed from ${oldValue} to ${newValue}`);
});
// 監(jiān)聽(tīng) age 屬性的變化
watch(age, (newValue, oldValue) => {
console.log(`Age changed from ${oldValue} to ${newValue}`);
});
總結(jié)
- 監(jiān)聽(tīng)單個(gè)屬性:使用
watch(() => state.name, ...)監(jiān)聽(tīng)特定屬性的變化。 - 監(jiān)聽(tīng)整個(gè)對(duì)象:使用
watch(state, ...)并結(jié)合deep: true深度監(jiān)聽(tīng)整個(gè)對(duì)象,并手動(dòng)檢查哪些屬性發(fā)生了變化。 - 監(jiān)聽(tīng)多個(gè)屬性:可以通過(guò)數(shù)組或組合
computed來(lái)監(jiān)聽(tīng)多個(gè)屬性的變化。 - 使用
toRefs:將reactive對(duì)象的屬性轉(zhuǎn)換為ref,然后分別進(jìn)行監(jiān)聽(tīng)。
這些方法可以幫助你靈活地監(jiān)聽(tīng) reactive 對(duì)象中的屬性變化,根據(jù)實(shí)際需求選擇合適的方式。
到此這篇關(guān)于Vue3監(jiān)聽(tīng)reactive對(duì)象中屬性變化的方法的文章就介紹到這了,更多相關(guān)Vue3監(jiān)聽(tīng)reactive屬性變化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3項(xiàng)目+element-plus:時(shí)間選擇器格式化方式
這篇文章主要介紹了vue3項(xiàng)目+element-plus:時(shí)間選擇器格式化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue.js項(xiàng)目在apache服務(wù)器部署問(wèn)題解決
本文主要介紹了Vue.js項(xiàng)目在apache服務(wù)器部署問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
在vue中使用screenfull?依賴,實(shí)現(xiàn)全屏組件方式
這篇文章主要介紹了在vue中使用screenfull?依賴,實(shí)現(xiàn)全屏組件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
vue中axios的post請(qǐng)求,415錯(cuò)誤的問(wèn)題
這篇文章主要介紹了vue中axios的post請(qǐng)求,415錯(cuò)誤的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
用vuex寫了一個(gè)購(gòu)物車H5頁(yè)面的示例代碼
本篇文章主要介紹了用vuex寫了一個(gè)購(gòu)物車H5頁(yè)面的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
Vue?使用postMessage?實(shí)現(xiàn)父子跨域通信
這篇文章主要介紹了Vue應(yīng)用?postMessage?實(shí)現(xiàn)父子跨域通信,通過(guò)示例介紹了postMessage的使用,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
vue項(xiàng)目中企業(yè)微信使用js-sdk時(shí)config和agentConfig配置方式詳解
這篇文章主要介紹了vue項(xiàng)目中企業(yè)微信使用js-sdk時(shí)config和agentConfig配置,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
vue-quill-editor二次封裝,實(shí)現(xiàn)自定義文件上傳方式
這篇文章主要介紹了vue-quill-editor二次封裝,實(shí)現(xiàn)自定義文件上傳方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

