elementUI二次确认按钮

本文介绍如何在 Element UI 的 el-button 中加入二次确认功能,通过 el-popconfirm 组件实现防止误操作的功能。

开发中需要用到二次确认按钮,防止误触等操作。

原代码为:

el-button @click="changeBtn()">修改</el-button>

(修改函数已省略)

加入二次确认功能后,代码改为:

<el-popconfirm title="这是一段内容确定删除吗?" @confirm="change()">
    <el-button slot="reference">修改</el-button>
</el-popconfirm>

其中,@confirm表示确认后触发的函数;@cancel表示取消后触发的函数(默认为取消后关闭二次提示框)

Vue 项目中实现点击删除附件按钮后弹出二次确认弹框,确认后调用删除接口的功能,以下以 Element UI 为例给出实现方法: #### 安装 Element UI 如果尚未安装 Element UI,可以使用以下命令进行安装: ```bash npm install element-ui ``` #### 引入 Element UI 在 `main.js` 中引入 Element UI: ```javascript import Vue from 'vue'; import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; import App from './App.vue'; Vue.use(ElementUI); new Vue({ render: h => h(App), }).$mount('#app'); ``` #### 实现代码 在组件中实现点击删除附件按钮弹出二次确认弹框并调用删除接口的功能: ```vue <template> <div> <button @click="handleDelete">删除附件</button> </div> </template> <script> export default { methods: { handleDelete() { this.$confirm('确定要删除该附件吗?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => { // 调用删除接口 this.deleteAttachment(); }).catch(() => { // 用户取消操作 this.$message({ type: 'info', message: '已取消删除' }); }); }, deleteAttachment() { // 模拟删除接口调用 // 这里需要替换为实际的接口地址和请求方式 // 例如使用 axios 发送请求 // axios.delete('/api/deleteAttachment', { params: { id: attachmentId } }) // .then(response => { // this.$message({ // type: 'success', // message: '附件删除成功' // }); // }) // .catch(error => { // this.$message({ // type: 'error', // message: '附件删除失败' // }); // }); console.log('调用删除接口'); } } }; </script> ``` 在上述代码中,点击“删除附件”按钮时会触发 `handleDelete` 方法,该方法会弹出二次确认弹框。用户点击“确定”后,会调用 `deleteAttachment` 方法模拟删除接口的调用;点击“取消”则会给出提示信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值