在创建弹框组件的时候,会遇到子组件的props更新到父组件的问题
这是父组件的代码
<Dialog :show.sync="showSafeDialog" title="安全防护措施和手段" width="50%">
<div v-html="means"></div>
</Dialog>
data() {
return {
showSafeDialog: false
}
},
子组件
<div class="dialog" v-if="show">
<div class="mainBox" :style="{width: width,left: ( 100 - parseInt(width)) / 2+'%',top: top}">
<div class="title">{{title}}</div>
<GeminiScrollbar style="max-height: 90%;">
<div class="content">
<slot></slot>
</div>
</GeminiScrollbar>
</div>
<div class="zhe" @click="close"></div>
</div>
<script>
export default {
name: "Dialog",
props: {
title: {
default: '',
type: String
},
width: {
default: '60%',
type: String
},
show: {
default: false,
type: Boolean
},
top: {
default: '20%',
type: String
}
},
methods: {
close() {
//This is 关键
this.$emit('update:show', false)
}
},
watch: {}
}
</script>
通过 this.$emit('update:**', 参数),这种方式更新了父组件的 showSafeDialog