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