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

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

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>
目录
相关文章
|
4天前
|
JavaScript 前端开发
Vue实现Element UI框架的自定义输入框或下拉框在输入时对列表选项进行过滤,以及右键列表选项弹出菜单进行删除
本文介绍了如何在Vue框架结合Element UI库实现自定义输入框或下拉框,在输入时对列表选项进行过滤,并支持右键点击列表选项弹出菜单进行删除的功能。
5 0
|
4天前
|
JavaScript 容器
Vue+Element UI
该博客文章介绍了如何在Vue中集成Element UI来构建后台管理系统的左侧菜单,包括使用`el-menu`、`el-submenu`和`el-menu-item`等组件,并通过Vue router动态构建菜单项及其路由设置。
|
1月前
|
JavaScript
vue + element UI 表单中内嵌自定义组件的表单校验触发方案
vue + element UI 表单中内嵌自定义组件的表单校验触发方案
42 5
|
2月前
|
XML IDE 开发工具
【Android UI】自定义带按钮的标题栏
【Android UI】自定义带按钮的标题栏
39 7
【Android UI】自定义带按钮的标题栏
|
1月前
|
前端开发 JavaScript
零基础学 Vue + Element UI 第01步 —— 搭建开发环境、创建项目、修改默认模板、启动项目、访问项目
零基础学 Vue + Element UI 第01步 —— 搭建开发环境、创建项目、修改默认模板、启动项目、访问项目
37 1
|
1月前
|
JavaScript
vue + element UI【实战】音乐播放器/语音条(内含音频的加载、控制,事件监听,信息获取,手机网页阴影的去除等技巧)
vue + element UI【实战】音乐播放器/语音条(内含音频的加载、控制,事件监听,信息获取,手机网页阴影的去除等技巧)
32 1
|
1月前
|
前端开发 JavaScript
vue + element-UI 图片压缩canvas【详解】(含完整demo)
vue + element-UI 图片压缩canvas【详解】(含完整demo)
134 1
|
1月前
|
JavaScript
element-ui 在vue中el-input输入框的autofocus属性失效【解决方案】
element-ui 在vue中el-input输入框的autofocus属性失效【解决方案】
96 1
|
1月前
|
JavaScript BI UED
vue + element UI【实战】打字闯关(含按键监听、按键音效、字符匹配、动态样式、结果判定、数据统计、音效获取和剪辑等实用技巧)
vue + element UI【实战】打字闯关(含按键监听、按键音效、字符匹配、动态样式、结果判定、数据统计、音效获取和剪辑等实用技巧)
30 0
|
1月前
|
JavaScript
vue项目中升级element ui(含常见报错及解决方案,如表格不显示,el-table无效, “__v_isRef“ is not defined,Use :deep() instead)
vue项目中升级element ui(含常见报错及解决方案,如表格不显示,el-table无效, “__v_isRef“ is not defined,Use :deep() instead)
23 0