详解vue父组件传递props异步数据到子组件的问题
作者:互联网
原文地址:https://www.cnblogs.com/goloving/p/9114389.html
案列:
父组件 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>
解决方法:
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>
标签:到子,vue,default,items,test,child,组件,childObject 来源: https://www.cnblogs.com/gteman/p/15011544.html