vue父組件異步傳遞props值,子組件接收不到解決方案
父組件異步請(qǐng)求數(shù)據(jù)之后傳值給子組件,子組件接收不到
貼上父組件代碼圖:
子組件代碼圖:
打印結(jié)果:
父組件異步請(qǐng)求數(shù)據(jù)回來在傳到子組件,子組件無法接收,打印的依然是默認(rèn)值。
這里有兩種解決思路
第一種:子組件使用watch監(jiān)聽
但是watch監(jiān)聽也會(huì)有接收不到值的情況,因?yàn)橐薷膒rops的favoriteValue的值,favoriteValue接收不到的話就無法修改,所以這里同時(shí)給favoriteValue、isfavorite賦值,此時(shí)favoriteValue沒值,但是data的isfavorite有值,所以后面的修改favoriteValue值的操作有效。
第二種:父組件使用v-if判斷
定義flag默認(rèn)值false,異步之后設(shè)為true是為了確保數(shù)據(jù)有值,這樣一來子組件里就不用watch監(jiān)聽數(shù)據(jù)啦
結(jié)果
綜上就是解決父組件異步傳遞props值,子組件接收不到的問題的解決方案,如果有更好的解決方案歡迎來撩
父組件傳遞props異步數(shù)據(jù)到子組件遇到的問題
狀況一
父組件parent.vue
// asyncData為異步獲取的數(shù)據(jù),想傳遞給子組件使用 <template> <div> 父組件 <child :child-data="asyncData"></child> </div> </template> <script> import child from '../demo/children.vue' export default { data: () => ({ asyncData: '' }), components: { child }, created () { }, mounted () { // setTimeout模擬異步數(shù)據(jù) setTimeout(() => { this.asyncData = ' async data' console.log('parent 組件結(jié)束') }, 2000) } } </script>
子組件child.vue
<template> <div> 子組件{{childData}} </div> </template> <script> export default { props: ['childData'], data: () => ({ }), created () { console.log("子組件created----- "+this.childData) // 空值 }, methods: { } } </script>
子組件的html中的{{childData}}的值會(huì)隨著父組件的值而改變
但是created里面的卻不會(huì)發(fā)生改變(生命周期問題)
案例二
parent.vue
<template> <div> 父組件 <child :child-object="asyncObject"></child> </div> </template> <script> import child from '../demo1/children.vue' export default { data: () => ({ asyncObject: '' }), components: { child }, created () { }, mounted () { // setTimeout模擬異步數(shù)據(jù) setTimeout(() => { this.asyncObject = {'items': [1, 2, 3]} console.log('parent 結(jié)束') }, 2000) } } </script>
子組件 children
<template> <div> 子組件<!--這里很常見的一個(gè)問題,就是{{childObject}}可以獲取且沒有報(bào)錯(cuò),但是{{childObject.items[0]}}不行,往往有個(gè)疑問為什么前面獲取到值,后面獲取不到呢?--> <p>{{childObject.items[0]}}</p> </div> </template> <script> export default { props: ['childObject'], data: () => ({ }), created () { console.log(this.childObject) // 空值 }, methods: { } } </script>
created里面的卻不會(huì)發(fā)生改變, 子組件的html中的{{{childObject.items[0]}}的值雖然會(huì)隨著父組件的值而改變,但是過程中會(huì)報(bào)錯(cuò)。
是因?yàn)椋菏紫葌鬟^來的是空,然后再異步刷新值,也就是開始時(shí)候childObject.items[0]等同于’’.item[0]這樣的操作,
所以就會(huì)報(bào)下面的錯(cuò):
解決辦法:
1、使用v-if可以解決報(bào)錯(cuò)問題和created為空問題
父組件parent
<template> <div> 父組件 <child :child-object="asyncObject" v-if="flag"></child> </div> </template> <script> import child from '../demo1/children.vue' export default { data: () => ({ asyncObject: '', flag:false }), components: { child }, created () { }, mounted () { // setTimeout模擬異步數(shù)據(jù) setTimeout(() => { this.asyncObject = {'items': [1, 2, 3]} this.flag= true console.log('parent 結(jié)束') }, 2000) } } </script>
子頁面 children
<template> <div> 子組件<!--這里很常見的一個(gè)問題,就是{{childObject}}可以獲取且沒有報(bào)錯(cuò),但是{{childObject.items[0]}}不行,往往有個(gè)疑問為什么前面獲取到值,后面獲取不到呢?--> <p>{{childObject.items[0]}}</p> </div> </template> <script> export default { props: ['childObject'], data: () => ({ }), created () { console.log("子組件create-----"+JSON.stringify(this.childObject)) // 空值 }, methods: { } } </script>
2、子組件使用watch來監(jiān)聽父組件改變的prop,使用methods來代替created
子組件 children
<template> <div> 子組件<!--這里很常見的一個(gè)問題,就是{{childObject}}可以獲取且沒有報(bào)錯(cuò),但是{{childObject.items[0]}}不行,往往有個(gè)疑問為什么前面獲取到值,后面獲取不到呢?--> <p>{{test}}</p> </div> </template> <script> export default { props: ['childObject'], data: () => ({ test: '' }), watch: { 'childObject.items': function (n, o) { this.test = n[0] this.updata() } }, methods: { updata () { // 既然created只會(huì)執(zhí)行一次,但是又想監(jiān)聽改變的值做其他事情的話,只能搬到這里咯 console.log(this.test)// 1 } } } </script>
3、子組件watch computed data 相結(jié)合(麻煩)
子組件children
<template> <div> 子組件<!--這里很常見的一個(gè)問題,就是{{childObject}}可以獲取且沒有報(bào)錯(cuò),但是{{childObject.items[0]}}不行,往往有個(gè)疑問為什么前面獲取到值,后面獲取不到呢?--> <p>{{test}}</p> </div> </template> <script> export default { props: ['childObject'], data: () => ({ test: '' }), watch: { 'childObject.items': function (n, o) { this._test = n[0] } }, computed: { _test: { set (value) { this.update() this.test = value }, get () { return this.test } } }, methods: { update () { console.log(this.childObject) // {items: [1,2,3]} } } } </script>
4、使用prop default來解決{{childObject.items[0]}}
父組件:
<template> <div> 父組件 <child :child-object="asyncObject"></child> </div> </template> <script> import child from '../demo4/children.vue' export default { data: () => ({ asyncObject: undefined // 這里使用null反而報(bào)0的錯(cuò) }), components: { child }, created () { }, mounted () { // setTimeout模擬異步數(shù)據(jù) setTimeout(() => { this.asyncObject = {'items': [1, 2, 3]} console.log('parent finish') }, 2000) } } </script>
子組件:
<template> <div> 子組件<!--1--> <p>{{childObject.items[0]}}</p> </div> </template> <script> export default { props: { childObject: { type: Object, default () { return { items: '' } } } }, data: () => ({ }), created () { console.log(this.childObject) // {item: ''} } } </script>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于vue開發(fā)的在線付費(fèi)課程應(yīng)用過程
這篇文章主要介紹了基于vue開發(fā)的在線付費(fèi)課程應(yīng)用過程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01rollup3.x+vue2打包組件的實(shí)現(xiàn)
本文主要介紹了rollup3.x+vue2打包組件的實(shí)現(xiàn),詳細(xì)的介紹了打包會(huì)存在的問題,包版本的問題,babel 轉(zhuǎn)換jsx等問題,具有一定的參考價(jià)值,感興趣的可以了解一下2023-03-03vue實(shí)現(xiàn)動(dòng)態(tài)綁定行內(nèi)樣式style的backgroundImage
這篇文章主要介紹了vue實(shí)現(xiàn)動(dòng)態(tài)綁定行內(nèi)樣式style的backgroundImage方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07前端elementUI?select選擇器實(shí)現(xiàn)遠(yuǎn)程搜索
這篇文章主要為大家介紹了前端使用elementUI?select選擇器實(shí)現(xiàn)遠(yuǎn)程搜索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05