APP..vue
<template>
<div>
我是子组件
<el-button type="primary" size="mini" @click="openA">打开ADialog</el-button>
<ADialog :avisible="visible" :key="key">
</ADialog>
</div>
</template>
<script>
import ADialog from '@/components/ADialog'
export default {
data() {
return {
visible:false,
key:Math.random(100)*100
};
},
components:{
ADialog
},
methods:{
openA(){
this.key = this.key +1
this.visible = true
}
}
}
</script>
<style>
</style>
ADialog.vue
<template>
<div v-if="visable">
<el-dialog
title="我是Adialog"
:visible.sync="visable"
width="30%"
@close="handlerClose"
>
<span>我要打开B了</span>
<el-button type="primary" size="default" @click="openB"
>打开Bdialog</el-button
>
<span slot="footer">
<el-button @click="visable = false">Cancel</el-button>
<el-button type="primary" @click="handlerOK">OK</el-button>
</span>
</el-dialog>
<BDialog :bvisable="bvisable" :key="key"></BDialog>
</div>
</template>
<script>
import BDialog from "@/components/BDialog";
export default {
props: ["avisible"],
data() {
return {
visable: this.avisible,
bvisable: false,
key:Math.random(100)*100
};
},
components: { BDialog },
methods: {
handlerClose() {
this.visable = false;
},
handlerOK() {
this.visable = false;
},
openB(){
this.key = this.key+1
this.bvisable = true
this.$forceUpdate()
}
},
};
</script>
<style>
</style>
BDialog.vue
<template>
<div v-if="visiable">
<el-dialog
title="我是Bdialog"
:visible.sync="visiable"
width="20%"
@close="handlerClose">
<span></span>
<span slot="footer">
<el-button @click="visiable = false">Cancel</el-button>
<el-button @click="ok">OK</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
props:[
"bvisable"
],
data() {
return {
visiable: this.bvisable,
};
},
methods:{
handlerClose(){
this.visiable = false
},
ok(){
this.visiable = false
}
}
}
</script>
<style>
</style>
在引用子组件的过程中加上:key就是给每一个vnode的唯一id,新的dialog就可以展示出来
父组件dialog中加上
子组件的dialog中加上
子组件调用的过程中加上:key