修改$confirm 的【确定】按钮的样式

在这里插入图片描述
需求:需要二次确认,但是按钮需要呈现为红色

 this.$confirm('驳回后可进行重新修改并提交!', {
   confirmButtonText: '确定驳回',
   cancelButtonText: '取消',
   confirmButtonClass: 'btnClass',
   type: 'warning'
 }).then(() => {
    console.log('点击确认页面!!!')
 })

btnClass的样式,

.btnClass{
  color: #fff;
  background-color: #F56C6C !important;
  border-color: #F56C6C !important;
}

注意: 按钮样式没有实现

<style scoped lang="scss">
</style>
把这里的”scoped“ 字段去掉即可实现,(确保标签内无其他样式,这个地方去掉 打包会成为公共样式)
scoped是为了实现组件的私有化,表示style只属于当前模块,不对全局造成污染。
### 更改 Vue `this.$confirm` 按钮样式或颜色 为了更改 `this.$confirm` 对话框中的按钮样式或颜色,可以利用 Element UI 提供的自定义类功能以及通过 CSS 进行更深入的定制。 #### 使用 `customClass` 属性应用全局样式 可以通过设置 `customClass` 参数来自定义整个对话框的样式。这允许添加特定于项目的 CSS 类,从而覆盖默认样式: ```javascript this.$confirm('删除后无法恢复,你还要继续吗?', '删除用户信息', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', customClass: 'my-custom-confirm' }).then(/* ... */).catch(/* ... */); ``` 在此基础上,在项目中加入相应的 CSS 定义[^3]: ```css .my-custom-confirm .el-button--primary { background-color: red !important; border-color: darkred !important; } .my-custom-confirm .el-button--default { color: blue !important; } ``` 上述代码片段展示了如何改变确认按钮 (`el-button--primary`) 和取消按钮 (`el-button--default`) 的背景色、边框色及文字颜色。 #### 动态调整单次调用样式的解决方案 如果希望仅针对某一次弹窗操作做特殊处理而不影响其他地方,则可以在创建消息时动态生成 HTML 结构并传递给 `$confirm` 方法作为 `message` 参数的一部分。这种方式需要用到 Vue 实例上的 `$createElement` 函数来构建 DOM 节点[^2]: ```javascript const h = this.$createElement; this.$confirm(h('div', [ h('p', { style: "color:red;" }, ['此操作将永久删除该文件']), h('br'), h('span', {}, ['是否继续?']) ]), '', { dangerouslyUseHTMLString: true, confirmButtonText: '<button class="special-btn">确定</button>', cancelButtonText: '<button class="cancel-btn">取消</button>' } ).then(/* ... */).catch(/* ... */); ``` 注意这里使用了 `dangerouslyUseHTMLString: true` 来告诉组件解析传入的内容为真实的 HTML 字符串而不是纯文本;同时也直接指定了带有额外样式按钮标签用于替换默认文本按钮。 对于更加复杂的场景,还可以考虑编写独立的消息框组件以实现完全可控的设计效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值