先說問題,父組件利用props向子組件傳值,瀏覽器 console 有這個(gè)值,但是獲取不到內(nèi)部的屬性,困了我3個(gè)小時(shí),真的**
personal
console
以下為原代碼
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對象賦值接口傳來的數(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行輸出卻沒有了,是不是超級奇怪
后面經(jīng)過大佬的講解,其實(shí)瀏覽器console.log也是應(yīng)該沒有的
所以,其實(shí)我們子組件一開始根本就沒有取到這個(gè)personal這個(gè)對象。
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對象賦值接口傳來的數(shù)據(jù) //使用以下方法重新賦值,上面方法watch監(jiān)聽不到,具體什么原因,我也不清楚,知道的告知我!謝謝 this.personal = { data: res.data.data, state: 'edit' } }) },}接下來子組件就能 watch 到 personal 了 子組件
watch:{ personal(newValue,oldValue){ console.log(181,newValue) },/** 輸出 { data: res.data.data, state: 'edit' }**/ }總結(jié)
以上所述是小編給大家介紹的解決vue組件props傳值對象獲取不到的問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
新聞熱點(diǎn)
疑難解答