ElementUI+Vue 解决在使用el-dialog时,点击el-dialog外的其他区域会导致该对话框关闭

需求描述

今天,在做Element+Vue项目时遇到一个需求:甲方要求在Dialog打开状态下,点击该Dialog以外的区域会导致该Dialog关闭;正确的状态应该是只有在点击关闭按钮,或者是Dialog内的其他操作性按钮才能使得Dialog的状态变为关闭。

弹窗关闭后还显示一层遮罩层,设置 append-to-body即可

问题分析

之所以会产生这种问题,是因为elementUi在对Dialog组件做初始化的时候,默认让该Dialog在点击组件以外区域会导致该组件关闭,所以elementUI 一定会将该属性暴露出来让开发人员进行配置。
通过查询ElementUI的官方文档,发现在Dialog下有个‘close-on-click-modal’属性,该属性默认值为‘True’,作用为:是否可以通过点击 modal 关闭 Dialog。
所以,通过设置Dialog下的close-on-click-modal属性为‘false’,即可解决该问题。

问题解决方式

解决方式一 : 将Dialog下的close-on-click-modal属性改为‘false’。
需要注意的是: 在使用close-on-click-modal属性时,必须在该属性前加“:”。
解决方式二: 可以通过before-close属性,在Dialog关闭时,让用户进行确认是否需要关闭。
before-close 仅当用户通过点击关闭图标或遮罩关闭 Dialog 时起效。如果你在 footer 具名 slot 里添加了用于关闭 Dialog 的按钮,那么可以在按钮的点击回调函数里加入 before-close 的相关逻辑

 代码实践

  • 解决方式一:
<el-button type="text" @click="dialogVisible = true">点击打开 Dialog</el-button>

<el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  :close-on-click-modal='false'>
  <span>这是一段信息</span>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
</el-dialog>

<script>
  export default {
    data() {
      return {
        dialogVisible: false
      };
    }
  };
</script>
  • 解决方式二:
<el-button type="text" @click="dialogVisible = true">点击打开 Dialog</el-button>

<el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
  <span>这是一段信息</span>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
</el-dialog>

<script>
  export default {
    data() {
      return {
        dialogVisible: false
      };
    },
    methods: {
      handleClose(done) {
        this.$confirm('确认关闭?')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      }
    }
  };
</script>

 

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的示例,演示如何使用 Vue3 和 Element Plus 封装一个弹窗组件: ```html <template> <el-dialog :title="title" :visible.sync="visible" :width="width" :before-close="beforeClose" > <slot></slot> <div slot="footer" class="dialog-footer"> <el-button @click="visible = false">取消</el-button> <el-button type="primary" @click="confirm">确定</el-button> </div> </el-dialog> </template> <script> import { defineComponent } from 'vue' import { ElDialog, ElButton } from 'element-plus' export default defineComponent({ name: 'MyDialog', components: { ElDialog, ElButton }, props: { title: { type: String, default: '提示' }, visible: { type: Boolean, default: false }, width: { type: String, default: '30%' }, beforeClose: Function, }, methods: { confirm() { // 触发确认事件,并关闭弹窗 this.$emit('confirm') this.visible = false }, }, }) </script> <style> /* 可选:自定义弹窗样式 */ .dialog-footer { padding: 10px 20px; text-align: right; background-color: #f5f7fa; border-top: 1px solid #ebeef5; } </style> ``` 使用方法: ```html <template> <div> <el-button @click="showDialog">打开弹窗</el-button> <my-dialog :visible.sync="dialogVisible" @confirm="handleConfirm" > <!-- 弹窗内容 --> <p>这是一个弹窗</p> </my-dialog> </div> </template> <script> import { defineComponent, ref } from 'vue' import MyDialog from '@/components/MyDialog.vue' export default defineComponent({ components: { MyDialog }, setup() { const dialogVisible = ref(false) const showDialog = () => { dialogVisible.value = true } const handleConfirm = () => { // 处理确认事件 console.log('确认') } return { dialogVisible, showDialog, handleConfirm } }, }) </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值