在一个vue页面,进入另一个弹出框定义的vue(传入dialogStatus值)时,我遇到第一次进入弹出框页面dialogStatus值为空,第二次进入时值传过去了。
代码:
vue页面:
<el-dialog :title="textMap[dialogStatus]" size="large" :visible.sync="dialogFormVisible">
<modify @closeStationDialog="closeStationDialog" :dialogStatus="dialogStatus" ref="modify"></modify>
</el-dialog>
export default {
components: {
'modify': () => import('./components/modify')
},
methods: {
handleAdd(){
this.dialogStatus = 'create';
this.dialogFormVisible = true;
console.log(this.$refs.modify);
if (this.$refs.modify !== undefined) {
this.$refs.modify.dialogStatus = this.dialogStatus;
}
}
}
}
modify页面(弹出框页面):
<div style="text-align: center; margin-bottom: 1rem;">
<el-button @click="cancel('form')">取 消</el-button>
<el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
<el-button v-else type="primary" @click="update('form')">确 定</el-button>
</div>
export default {
data(){
return{
dialogStatus: ''
}
}
}
解决办法:将 dialogStatus(需要传递的值)定义在 props 里。
export default {
props: {
dialogStatus: {
default: '1'
}
}
}
来自vue.js官网的介绍:vue.js
组件实例的作用域是孤立的。这意味着不能 (也不应该) 在子组件的模板内直接引用父组件的数据。父组件的数据需要通过 prop 才能下发到子组件中。