vue3如何使用watch監(jiān)聽props中的數(shù)據(jù)
情況一:監(jiān)聽 props 中基本數(shù)據(jù)類型
父組件中對傳入數(shù)據(jù)的處理
const handleClick = () => {
testStr.value += 'P'
}
子組件中監(jiān)聽傳入的數(shù)據(jù)
watch(
() => props.testStr,
(newVal, oldVal) => {
console.log('監(jiān)聽基本類型數(shù)據(jù)testStr')
console.log('new', newVal)
console.log('old', oldVal)
}
)
不能使用
watch(
props.testStr,
() => {
console.log('監(jiān)聽基本類型數(shù)據(jù)testStr')
}
)
的形式,要使用 getter 函數(shù)返回值的形式才能觸發(fā)監(jiān)聽
情況二:監(jiān)聽 props 中引用數(shù)據(jù)類型且父組件不改變地址指向
父組件中對傳入數(shù)據(jù)的處理
const handleClick = () => {
let name = 'lx'
let age = 18
dataList.value.push({
name: (name += '~'),
age: (age += 1)
})
}
子組件中監(jiān)聽傳入的數(shù)據(jù)
watch(props.dataList, (newVal, oldVal) => {
console.log('監(jiān)聽引用類型數(shù)據(jù)dataList')
console.log('new', newVal)
console.log('old', oldVal)
})
當(dāng)父組件傳入的是引用類型數(shù)據(jù),且在父組件中沒有改變該數(shù)據(jù)的引用地址時,在子組件中可以直接監(jiān)聽傳入的數(shù)據(jù)
情況三:監(jiān)聽 props 中引用數(shù)據(jù)類型且父組件改變地址指向
父組件中對傳入數(shù)據(jù)的處理
const handleClick = () => {
let name = 'lx'
let age = 18
dataList.value=[
{
name: (name += '~'),
age: (age += 1)
}
]
}
子組件中監(jiān)聽傳入的數(shù)據(jù)
watch(
() => props.dataList,
(newVal, oldVal) => {
console.log('監(jiān)聽引用類型數(shù)據(jù)dataList')
console.log('new', newVal)
console.log('old', oldVal)
}
)
當(dāng)父組件傳入的是引用類型數(shù)據(jù),且在父組件中通過賦值的形式,改變引用數(shù)據(jù)的引用地址時,在子組件中要使用 getter 函數(shù)返回值的形式,才能監(jiān)聽傳入的數(shù)據(jù)
總結(jié)
- watch監(jiān)聽 props 中的基本類型數(shù)據(jù),需要通過 getter 函數(shù)返回值的形式(()=>props.xxx)才能監(jiān)聽
- watch監(jiān)聽 props 中的引用類型數(shù)據(jù),且父組件中沒有改變地址指向時,可以直接監(jiān)聽
- watch監(jiān)聽 props 中的引用類型數(shù)據(jù),且父組件中改變了地址指向時,需要通過 getter 函數(shù)返回值的形式(()=>props.xxx)才能監(jiān)聽
- 開發(fā)情景:要做瀑布流展示
定義變量:const dataList = ref([])
父組件從接口獲取第一頁數(shù)據(jù),將數(shù)據(jù)存在dataList中:dataList.value = res.data,注意:此時,已經(jīng)改變引用類型數(shù)據(jù) dataList 的地址指向
子組件通過watch監(jiān)聽傳入的 dataList,并且調(diào)用 manageData() 方法處理 props.dataList 的數(shù)據(jù)結(jié)構(gòu):
watch(
() => props.dataList,
() => {
console.log('監(jiān)聽引用類型數(shù)據(jù)dataList')
manageData()
... // 相應(yīng)邏輯處理
}
)
注意:此時可以觸發(fā)監(jiān)聽
用戶下拉刷新操作后,繼續(xù)發(fā)送接口,獲取第二頁,第三頁等等后面的數(shù)據(jù),父組件通過 push 操作,將獲取到的數(shù)據(jù) push 進(jìn) dataList 中:
for(let item of res.data){
dataList.value.push(item)
}
注意:此時,雖然父組件傳入的 dataList 的值修改了,但是子組件已經(jīng)不能觸發(fā)watch及其處理邏輯了
也就是說,manageData() 方法不能調(diào)用,沒有做到對后續(xù) push 進(jìn)來的數(shù)據(jù)進(jìn)行數(shù)據(jù)結(jié)構(gòu)的修改,導(dǎo)致頁面展示出現(xiàn)問題
解決辦法:
一、使用 computed
const dataListTest = computed(() => {
manageData()
return props.dataList
})
二、使用 watchEffect
watchEffect(() => {
manageData()
})
寫在最后
最后的瀑布流展示中,我是直接修改了 props 中的數(shù)據(jù),雖然從展示來說沒有發(fā)現(xiàn)什么問題,但是在 Vue 的官網(wǎng)中是這樣說的:

PS:本例子使用語法糖 script setup
到此這篇關(guān)于vue3如何使用watch監(jiān)聽props中數(shù)據(jù)的文章就介紹到這了,更多相關(guān)vue3監(jiān)聽props數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE搭建分布式醫(yī)療掛號系統(tǒng)的前臺預(yù)約掛號步驟詳情
這篇文章主要介紹了VUE搭建分布式醫(yī)療掛號系統(tǒng)的前臺預(yù)約掛號步驟詳情,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
elementUI table表格動態(tài)合并的示例代碼
這篇文章主要介紹了elementUI table表格動態(tài)合并的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
vue3使用element-plus搭建后臺管理系統(tǒng)之菜單管理功能
這篇文章主要介紹了vue3使用element-plus搭建后臺管理系統(tǒng)之菜單管理,使用element-plus el-tree組件快速開發(fā)樹形菜單結(jié)構(gòu),el-tree組件中filter-node-method事件便可以實現(xiàn)樹形菜單篩選過濾功能,需要的朋友可以參考下2022-04-04
antd design table更改某行數(shù)據(jù)的樣式操作
這篇文章主要介紹了antd design table更改某行數(shù)據(jù)的樣式操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Vue.extend實現(xiàn)組件庫message組件示例詳解
這篇文章主要為大家介紹了Vue.extend實現(xiàn)組件庫message組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

