vue实现弹窗子组件打开关闭的几种方法

方法一:直接在父组件内操作子组件的变量

父组件:

父组件:

<template>
    <div>
        <el-button @click="lookChild">查看</el-button>
        <child ref="childDialog" />
    </div>
</template>
<script>
import child from './components/dialog/child'
export default {
    methods: {
        lookChild() {
            this.$refs.childDialog.showDialog = true
        }
    }
}
</script>

子组件:
<template>
    <el-dialog
      title="弹窗标题"
      :visible.sync="showDialog"
      width="80%"
      center
    >
        弹窗内容
    </el-dialog>
</template>
<script>
export default {
    data() {
        return {
            showDialog: false
        }
    }
}
</script>

(耦合度太高,不推荐直接操作子组件)

方法二:通过v-if控制弹窗组件的销毁,重建

父组件:

<template>
    <div>
        <el-button @click="lookChild">查看</el-button>
        <child v-if=“showChildDialog” @dialogClose="dialogClose" />
    </div>
</template>
<script>
import child from './components/dialog/child'
export default {
    data() {
        return {
            showChildDialog: false
        }
    },
    methods: {
        lookChild() {
            this.showChildDialog = true
        },
        /** 关闭弹窗 */
        dialogClose() {
            this.showChildDialog = false
        }
    }
}
</script>

子组件:
<template>
    <el-dialog
      title="弹窗标题"
      :visible.sync="showDialog"
      width="80%"
      center
      @close="dialogClose"
    >
        弹窗内容
    </el-dialog>
</template>
<script>
export default {
    data() {
        return {
            showDialog: true
        }
    },
    methods: {
        dialogClose() {
            this.$emit('dialogClose')
        }
    }
}
</script>

(v-if会销毁,重建弹窗,若弹窗内容较多,重建会增加大量渲染开销)

方法三:父子组件传值(推荐)

父组件:

<template>
    <div>
        <el-button @click="lookChild">查看</el-button>
        <child :show-child-dialog.sync="showDialog" />
    </div>
</template>
<script>
import child from './components/dialog/child'
export default {
    data() {
        return {
            showDialog: false
        }
    },
    methods: {
        lookChild() {
            this.showDialog = true
        }
    }
}
</script>

子组件:
<template>
    <el-dialog
      title="弹窗标题"
      :visible.sync="showDialog"
      width="80%"
      center
    >
        弹窗内容
    </el-dialog>
</template>
<script>
export default {
    props: {
        showChildDialog: {
            type: Boolean,
            default: false
        }
    },
    computed: {
        showDialog: {
            get: function() {
                return this.showChildDialog
            },
            set: function(newValue) {
                this.$emit('update:showChildDialog', newValue) // 触发更新事件,父组件的showDialog会自动更新
            }
        }
    }
}
</script>
  • .sync修饰符:它会被扩展为一个自动更新父组件属性的 v-on 监听器
  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值