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

案例一
html

  父組件parent.vuevue

// 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.vuegit

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

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

案例二
異步

  parent.vueasync

<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.vueide

<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]}}的值雖然會隨着父組件的值而改變,可是過程當中會報錯ui

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

針對二的解決方法:this

一、使用v-if能夠解決報錯問題,和created爲空問題spa

// 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>

二、子組件使用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>

三、子組件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>

四、使用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()的東西供這兩個組件鏈接),才能相互觸發

五、使用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>
相關文章
相關標籤/搜索