[轉]詳解vue父組件傳遞props異步數據到子組件的問題


原文地址:https://www.cnblogs.com/goloving/p/9114389.html

案例一

  父組件parent.vue

復制代碼
// asyncData為異步獲取的數據,想傳遞給子組件使用 <template> <div> 父組件 <child :child-data="asyncData"></child> </div> </template> <script> import child from './child' export default { data: () => ({ asyncData: '' }), components: { child }, created () { }, mounted () { // setTimeout模擬異步數據 setTimeout(() => { this.asyncData = 'async data' console.log('parent finish') }, 2000) } } </script>
復制代碼

  子組件child.vue

復制代碼
<template>
 <div> 子組件{{childData}} </div> </template> <script> export default { props: ['childData'], data: () => ({ }), created () { console.log(this.childData) // 空值  }, methods: { } } </script>
復制代碼

  上面按照這里的解析,子組件的html中的{{childData}}的值會隨着父組件的值而改變,但是created里面的卻不會發生改變(生命周期問題)

案例二

  parent.vue

復制代碼
<template>
 <div> 父組件 <child :child-object="asyncObject"></child> </div> </template> <script> import child from './child' export default { data: () => ({ asyncObject: '' }), components: { child }, created () { }, mounted () { // setTimeout模擬異步數據 setTimeout(() => { this.asyncObject = {'items': [1, 2, 3]} console.log('parent finish') }, 2000) } } </script>
復制代碼

  child.vue

復制代碼
<template>
 <div> 子組件<!--這里很常見的一個問題,就是{{childObject}}可以獲取且沒有報錯,但是{{childObject.items[0]}}不行,往往有個疑問為什么前面獲取到值,后面獲取不到呢?--> <p>{{childObject.items[0]}}</p> </div> </template> <script> export default { props: ['childObject'], data: () => ({ }), created () { console.log(this.childObject) // 空值  }, methods: { } } </script>
復制代碼

  created里面的卻不會發生改變, 子組件的html中的{{{childObject.items[0]}}的值雖然會隨着父組件的值而改變,但是過程中會報錯

// 首先傳過來的是空,然后在異步刷新值,也開始時候childObject.items[0]等同於''.item[0]這樣的操作,
所以就會報下面的錯 vue.esm.js?8910:434 [Vue warn]: Error in render function:
"TypeError: Cannot read property '0' of undefined"

針對二的解決方法:

1、使用v-if可以解決報錯問題,和created為空問題

復制代碼
// parent.vue <template> <div> 父組件 <child :child-object="asyncObject" v-if="flag"></child> </div> </template> <script> import child from './child' export default { data: () => ({ asyncObject: '', flag: false }), components: { child }, created () { }, mounted () { // setTimeout模擬異步數據 setTimeout(() => { this.asyncObject = {'items': [1, 2, 3]} this.flag = true console.log('parent finish') }, 2000) } } </script>
復制代碼
復制代碼
//child.vue <template> <div> 子組件//不報錯 <p>{{childObject.items[0]}}</p> </div> </template> <script> export default { props: ['childObject'], data: () => ({ }), created () { console.log(this.childObject)// Object {items: [1,2,3]}  }, methods: { } } </script>
復制代碼

2、子組件使用watch來監聽父組件改變的prop,使用methods來代替created

復制代碼
<template>
 <div> 子組件<!--1--> <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只會執行一次,但是又想監聽改變的值做其他事情的話,只能搬到這里咯 console.log(this.test)// 1  } } } </script>
復制代碼

3、子組件watch computed data 相結合,有點麻煩

復制代碼
<template>
 <div> 子組件<!--這里很常見的一個問題,就是{{childObject}}可以獲取且沒有報錯,但是{{childObject.items[0]}}不行,
往往有個疑問為什么前面獲取到值,后面獲取不到呢?--> <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、使用emit,on,bus相結合

復制代碼
<template>
 <div> 子組件 <p>{{test}}</p> </div> </template> <script> export default { props: ['childObject'], data: () => ({ test: '' }), created () { // 綁定 this.$bus.on('triggerChild', (parmas) => { this.test = parmas.items[0] // 1 this.updata() }) }, methods: { updata () { console.log(this.test) // 1  } } } </script>
復制代碼

  這里使用了bus這個庫,parent.vue和child.vue必須公用一個事件總線(也就是要引入同一個js,這個js定義了一個類似let bus = new Vue()的東西供這兩個組件連接),才能相互觸發

5、使用prop default來解決{{childObject.items[0]}}

復制代碼
<template>
 <div> 父組件 <child :child-object="asyncObject"></child> </div> </template> <script> import child from './child' export default { data: () => ({ asyncObject: undefined // 這里使用null反而報0的錯  }), components: { child }, created () { }, mounted () { // setTimeout模擬異步數據 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>
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM