问题:父子组件传值,子组件数据只更新一次,后面再次更改父组件的数据,子组件不再更新
描述:
html:
父组件:
<el-form-item v-if="showInfo" label="XXX">
<list-info :allInfo="allInfo"></list-info>
</el-form-item
子组件:
<!--信息-->
<template>
<el-row>
<el-col :span="8">
<span>编码:</span>
<span>{{aInfo.code}}</span>
</el-col>
<el-col :span="8">
<span>名称:</span>
<span>{{aInfo.name}}</span>
</el-col>
<el-col :span="8">
<span>内容:</span>
<span>{{aInfo.content}}</span>
</el-col>
</el-row>
</template>
<script>
export default {
data () {
return {
aInfo: ''
};
},
props: {
allInfo: {
type: Object,
default: () => {}
}
},
mounted () { // 在mounted时候赋值,子组件只更新一次,后面重新选择后展示此组件的数据,不再更新
this.aInfo = this.allInfo
}
watch: { // 监听到数据然后赋值
allInfo (newV, oldV) {
this.aInfo = JSON.parse(JSON.stringify(newV))
}
}
}
</script>
解决方案:将mounted时候赋值改成watch监听数据再给子组件赋值