ElementPlus主题色修改

一般情况下我们使用elementplus的时候 肯定需要更改他的主题颜色,我们系统中的主题颜色大多数的时候适合他的主题颜色不一样的。
那么如何更改呢?
我是在assets/styles/element/index.scss下

@forward 'element-plus/theme-chalk/src/common/var.scss' with (
  $colors: (
    'primary': (
      'base': #427cff
    ),
    'success': (
      'base': #009817
    ),
    'warning': (
      'base': #e27d02
    ),
    'danger': (
      'base': #fb4a32
    ),
    'error': (
      'base': #ff5f5f
    ),
    'info': (
      'base': #65676f
    )
  ),
  $text-color: (
    'primary': #646a74,
    'regular': #edf3fc,
    'secondary': #333333,
    'placeholder': #9598a3,
    'disabled': #dcdee4
  ),
);

@use 'element-plus/theme-chalk/src/index.scss' as *;

想要全局使用的话,在vite.config.ts中

css: {
    preprocessorOptions: {
      scss: {
        additionalData: `
          @use "@/assets/styles/element/index.scss" as *;
          `
      }
    }
}

解释:
@forward
  通过@forward加载一个模块的成员,并将这些成员当作自己的成员对外暴露出去,类似于类似于 es6 的 export …,通常用于跨多个文件组织 Sass 库
(相当于中转站,通过@forward引入scss文件并将引入scss文件中的变量、混合、函数等抛出,当其
他scss文件用@use引入此模块时可使用)

@use与@forward一起使用的情况
当一个模块里面须要同时使用@use与@forward时,先使用@forwar后再使用@use

@use
  注意:@use必须声明在其他代码之上
使用总结
@use引入同一个文件多次,不会重复引入,而@import会重复引入
@use引入的文件都是一个模块,默认以文件名作为模块名,可通过as alias取别名
@use引入多个文件时,每个文件都是单独的模块,相同变量名不会覆盖,通过模块名访问,而 @import变量会被覆盖
@use方式可通过 @use ‘xxx’ as *来取消命名空间,建议不要这么做
@use模块内可通过− 或 - 或−或来定义私有成员,也就是说或者-开头的Variables mixins functions 不会被引入
@use模块内变量可通过!default 定义默认值,引入时可通用with(…)的方式修改
可定义-index.scss或_index.scss来合并多个scss文件,它@use默认加载文件

参考链接

第二种方法
最后换了一种方式,写一个hooks

import { onMounted } from 'vue';

export const setElementTheme = () => {
  onMounted(() => {
    setStyle('--el-color-primary', '#14C2C2');
    setStyle('--el-text-color-regular', '#000000d9');
    setStyle('--el-text-color-placeholder', '#00000040');
  });

  const setStyle = (key: string, value: string) => {
    document.documentElement.style.setProperty(key, value);
  };
};

在app.vue中

import { setElementTheme } from '@/hooks/setElementTheme';

setElementTheme();

第三种
在styles下面写一个index.scss中直接覆盖变量,然后再main.ts中引入

import './assets/styles/index.scss';

index.scss

/* 修改主题色 */
:root {
  --el-color-primary: #05B7BFFF;
  --el-text-color-regular: #000000d9;
  --el-text-color-placeholder: #00000040;
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值