【Element-UI】vue使用 this.$confirm区分取消与关闭,vue给this.$confirm设置多个按钮

在使用this.$confirm过程中经常会遇到将取消按钮修改成其他的按钮去执行,如果直接使用catch会出现右上角以及遮罩层关闭也会执行,所以需要做个区分
如遇到两个按钮可以用取消按钮作为第二个按钮如果是出现多个按钮就不支持了

vue使用 this.$confirm时区分取消与右上角关闭和弹窗关闭

html代码

<template>
  <el-button type="text" @click="open">点击打开 Message Box</el-button>
</template>

js代码

<script>
  export default {
    methods: {
      open() {
        this.$confirm('检测到未保存的内容,是否在离开页面前保存修改?', '确认信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '保存',
          cancelButtonText: '放弃修改'
        })
          .then(() => {
            this.$message({
              type: 'info',
              message: '保存修改'
            });
          })
          .catch(action => {
            this.$message({
              type: 'info',
              message: action === 'cancel'
                ? '放弃保存并离开页面'
                : '停留在当前页面'
            })
          });
      }
    }
  }
</script>

全部代码

<template>
  <el-button type="text" @click="open">点击打开 Message Box</el-button>
</template>

<script>
  export default {
    methods: {
      open() {
        this.$confirm('检测到未保存的内容,是否在离开页面前保存修改?', '确认信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '保存',
          cancelButtonText: '放弃修改'
        })
          .then(() => {
            this.$message({
              type: 'info',
              message: '保存修改'
            });
          })
          .catch(action => {
            this.$message({
              type: 'info',
              message: action === 'cancel'
                ? '放弃保存并离开页面'
                : '停留在当前页面'
            })
          });
      }
    }
  }
</script>

vue给this.$confirm设置多个按钮

Vue中使用this.confirm时,虽然默认只提供了确认和取消两个按钮,但您可以通过自定义Vue.prototype上的confirm方法或者直接使用第三方UI库(如Element UI、Vuetify等)提供的API来自定义对话框,以包含更多的操作按钮。以下是一个基于Element UI的示例,展示如何在this.$confirm中设置多个操作按钮:

使用Element UI自定义确认对话框
Element UI并没有直接提供在this.$confirm中设置多个按钮的选项,但您可以使用el-dialog组件来自定义一个具有多个操作按钮的对话框。下面是一个基本示例:

HTML代码

<template>
  <el-dialog
    :title="'系统提示'"
    :visible.sync="dialogVisible"
    width="30%"
    @close="handleClose"
  >
    <p>页面内已存在正在编辑的模型</p>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleEdit">继续编辑</el-button>
      <el-button @click="handleCreateNew">创建新模型</el-button>
      <el-button @click="handleClose">关闭</el-button>
    </span>
  </el-dialog>
</template>

js代码

<script>
export default {
  data() {
    return {
      dialogVisible: false,
    };
  },
  methods: {
    // 显示自定义对话框
    showDialog() {
      this.dialogVisible = true;
    },
    // 继续编辑操作
    handleEdit() {
      this.$emit('update', { page_id: this.page_id });
      this.dialogVisible = false;
    },
    // 创建新模型操作
    handleCreateNew() {
      this.add_page();
      this.dialogVisible = false;
    },
    // 关闭对话框
    handleClose() {
      this.dialogVisible = false;
    },
  },
};
</script>

全部代码

<template>
  <el-dialog
    :title="'系统提示'"
    :visible.sync="dialogVisible"
    width="30%"
    @close="handleClose"
  >
    <p>页面内已存在正在编辑的模型</p>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleEdit">继续编辑</el-button>
      <el-button @click="handleCreateNew">创建新模型</el-button>
      <el-button @click="handleClose">关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
    };
  },
  methods: {
    // 显示自定义对话框
    showDialog() {
      this.dialogVisible = true;
    },
    // 继续编辑操作
    handleEdit() {
      this.$emit('update', { page_id: this.page_id });
      this.dialogVisible = false;
    },
    // 创建新模型操作
    handleCreateNew() {
      this.add_page();
      this.dialogVisible = false;
    },
    // 关闭对话框
    handleClose() {
      this.dialogVisible = false;
    },
  },
};
</script>
Element UI的`this.$confirm`组件中,`message`属性用于设置确认消息的内容,通常是一个提示用户需要确认的操作的文字描述。默认情况下,这个消息会显示在上方,而两个操作按钮(通常是"确定"和"取消")会放在下方。 如果你想改变它们的位置,让按钮在上、消息在下,你可以通过自定义插槽(slot)来实现。在Vue模板中,可以在`.el-dialog__footer`这一部分添加你的自定义内容,将按钮替换到`.dialog-confirm__buttons`里面,并将确认消息插入到其他地方,例如`.dialog-confirm__message`。这里是一个简单的例子: ```html <template> <div> <el-dialog :visible.sync="dialogVisible" class="custom-confirm-dialog" > <div slot="footer"> <span class="custom-message">{{ message }}</span> <el-button @click="handleCancel">取消</el-button> <el-button type="primary" @click="handleConfirm">确定</el-button> </div> </el-dialog> </div> </template> <script> export default { data() { return { dialogVisible: false, message: '这是一条确认信息', }; }, methods: { handleConfirm() { // 确定操作 }, handleCancel() { // 取消操作 }, }, }; </script> <style scoped> .custom-confirm-dialog .dialog-confirm__message { /* 自定义样式 */ display: flex; align-items: center; justify-content: center; } .custom-confirm-dialog .custom-message { /* 将消息移动到原来按钮的位置 */ position: absolute; bottom: 0; } </style> ``` 请注意,你需要根据实际的需求和Element UI的版本调整CSS样式。如果Element UI提供官方支持的自定义布局选项,建议查阅其文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

G佳伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值