前提:如果你的项目也使用了 SCSS, 那么你可以直接修改Element Plus的样式变量。 新建一个样式文件,例如
styles/element/index.scss:
添加配置样式的文件:style/element/index.scss
@forward "element-plus/theme-chalk/src/common/var.scss" with (
$colors: (
"primary": (
"base": #179bd5,
),
"success": (
"base": #67C23A,
),
"warning": (
"base": #E6A23C,
),
"danger": (
"base": #F56C6C,
),
"error": (
"base": #F56C6C,
),
"info": (
"base": #909399,
),
),
$button-padding-horizontal: (
"default": 80px
)
);
引入自定义的样式
在vite.config.ts中配置
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import viteCompression from 'vite-plugin-compression'
// https://vitejs.dev/config/
export default defineConfig({
base: './', //打包路径
plugins: [
vue(),
AutoImport({
resolvers: [
ElementPlusResolver({
// 关键:自动引入修改主题色添加这一行,使用预处理样式,不添加将会导致使用ElMessage,ElNotification等组件时默认的主题色会覆盖自定义的主题色
importStyle: 'sass'
})
]
}),
Components({
resolvers: [
ElementPlusResolver({
// 关键:自动引入修改主题色添加这一行,使用预处理样式
importStyle: 'sass'
})
]
}),
// gzip压缩 生产环境生成 .gz 文件
viteCompression({
verbose: true,
disable: false,
threshold: 10240,
algorithm: 'gzip',
ext: '.gz'
})
],
css: {
preprocessorOptions: {
scss: {
additionalData: `@use "@/style/element/index.scss" as *;` //关键
}
}
},
resolve: {
alias: {
'@': path.resolve(__dirname, 'src')
}
}
})