1.子组件获取不到父组件异步加载的值
父组件:
<template>
<child :info="childInfo"></child>
</template>
<script>
export default {
components: {
Child
},
data: function () {
return {
childInfo:'1'
}
},
created () {
this.getInfo()
},
mounted () {},
methods: {
getGoodInfo: function () {
this.$axios
.get(
'http://localhost:8000/getInfo'
)
.then(response => {
this.getInfo= response.data.info
})
}
}
</script>
由于数据是异步获取的,而子组件在一开始就渲染了,所以这时候info是获取不到数据的,解决方法:
<child v-if="childInfo != '1' " :info="childInfo"></child>
用v-if加个判断,当有值时才获取。