详解vue2父组件传递props异步数据到子组件的问题

测试环境:vue v2.3.3, vue v2.3.1

案例一

父组件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"

针对二的解决方法:

使用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>

子组件使用watch来监听父组件改变的prop,使用methods来代替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> 子组件<!--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 相结合,有点麻烦

parent.vue

<template> <div> 父组件 <child :child-object="asyncObject"></child> </div> </template> <script> import child from './child' export default { data: () => ({ asyncObject: undefined }), components: { child }, created () { }, mounted () { // setTimeout模拟异步数据 setTimeout(() => { this.asyncObject = {'items': [1, 2, 3]} console.log('parent finish') }, 2000) } } </script>

child.vue

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyffzs.html