Vue ElementUI 修改消息提示框样式—messageBox 的大小

在窄屏模式下(移动端或pda),提示框的宽度太宽,会出现显示不完全的问题。 应当如何修改 ElementUI 的样式呢?

javascript 复制代码
  open() {
      this.$confirm(
        window.vm.$i18n.t("tips.conLogOut"),
        window.vm.$i18n.t("tips.tip"),
        {
          confirmButtonText: window.vm.$i18n.t("backTips.confirm"),
          cancelButtonText: window.vm.$i18n.t("backTips.cancel"),
          type: "warning",
        }
      ).then(() => {
        this.logout();
      });
    },
css 复制代码
 <style scoped>
  .el-message-box {
    width: 235px;
}
</style>


此时在scoped的style中写是无效的,因为ElementUI组件不可以给样式添加scoped,因此必须去掉scoped;但是去掉scoped后不满足单组件的CSS。

解决方案

1、附加在没有scoped的style中

css 复制代码
<style scoped>
  ...
</style>
<style>
  ...
  .el-message-box {
    width: 235px;
  }
</style>


2、给消息提示框加类名(荐)
更加推荐这个messageBox添加一个类名,比较好用并且不会影响到其他页面的弹框样式。

javascript 复制代码
this.$confirm('确认注销吗?', '提示', {
  customClass: 'elmessageWidth'
}).then(() => {
  this.$message({
    message: '已成功注销',
    type: 'success'
  })
}).catch(() => {  })
css 复制代码
<style scoped>
  ...
</style>
<style>
 .elmessageWidth {
    width: 350px;
  }
</style>

或者直接important

css 复制代码
@media (max-width: 730px) {
  .elmessageWidth{
    width: 350px !important;
  }
 }
相关推荐
suedar11 分钟前
React 16 + TDesign Table 卡死问题深度复盘
前端
浩星1 小时前
「Vue3 + Cesium 最佳实践」完整工程化方案
前端·javascript·vue.js
小李子呢02111 小时前
前端八股Vue(5)---v-if和v-show
前端·javascript·vue.js
yuki_uix1 小时前
跨域与安全:CORS、HTTPS 与浏览器安全机制
前端·面试
用户3153247795451 小时前
React19项目中 FormEdit / FormEditModal 组件封装设计说明
前端·react.js
陆枫Larry1 小时前
Git 合并冲突实战:`git pull` 失败与 `pull.ff=only` 的那些事
前端
江南月1 小时前
让智能体边想边做:从 0 理解 ReActAgent 的工作方式
前端·人工智能
YiuChauvin1 小时前
vue2中使用 AntV G6
javascript·vue.js
袋鱼不重1 小时前
Hermes Agent 安装与实战:从安装到与 OpenClaw 全方位对比
前端·后端·ai编程