以下為原代碼
1、home.vue(父組件)--personal是被傳的參數(shù)
<!--子組件--> <form-picker class="form-picker" :personal="personal" > </form-picker> export default { data(){ return{ personal:{ state:'',////判斷是修改狀態(tài),還是新增狀態(tài) add/edit data:[] } } }, mounted(){ this.$api.personal.searchPersonalInfo(this.userInfo.userId).then((res)=>{ this.personal.data = res.data.data //這里給personal對(duì)象賦值接口傳來(lái)的數(shù)據(jù) }) }, }
2、formPicker (子組件) --接收personal
export default { props:['active','personal'], mounted(){ console.log(149,this.personal) console.log(150,this.personal.state) } }
運(yùn)行結(jié)果
明明149行有 state 值,150行輸出卻沒(méi)有了,是不是超級(jí)奇怪
后面經(jīng)過(guò)大佬的講解,其實(shí)瀏覽器console.log也是應(yīng)該沒(méi)有的
所以,其實(shí)我們子組件一開始根本就沒(méi)有取到這個(gè)personal這個(gè)對(duì)象。
3、解決方法--使用watch
父組件
export default { data(){ return{ personal:{ state:'',////判斷是修改狀態(tài),還是新增狀態(tài) add/edit data:[] } } }, mounted(){ this.$api.personal.searchPersonalInfo(this.userInfo.userId).then((res)=>{ //this.personal.data = res.data.data //這里給personal對(duì)象賦值接口傳來(lái)的數(shù)據(jù) //使用以下方法重新賦值,上面方法watch監(jiān)聽(tīng)不到,具體什么原因,我也不清楚,知道的告知我!謝謝 this.personal = { data: res.data.data, state: 'edit' } }) }, }
接下來(lái)子組件就能 watch 到 personal 了 子組件
watch:{ personal(newValue,oldValue){ console.log(181,newValue) }, /**
總結(jié)
以上所述是小編給大家介紹的解決vue組件props傳值對(duì)象獲取不到的問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com