element-plus 自动引入修改主题色

简介: element-plus 自动引入修改主题色

vue3出来了,想尝鲜首先想到的依然是element。

一上来就遇到如何自定义主题色的问题。

element-plus官网给了两个示例,完整引入的和手动引入的,唯独缺了自动引入的。

整了好些天才整出来一个解决方法供大家参考,避免踩坑。

安装依赖

# sass
npm install -D sass
# element-plus官方文档中自动引入所需的两个依赖
npm install -D unplugin-vue-components unplugin-auto-import

打包工具

我使用的是vite,此处依然是以vite配置为例

开始

第一步、照着element-plus官网给的方法配置自动引入

附上官网地址element-plus按需导入官网示例

// vite.config.ts
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
export default {
  plugins: [
    // ...
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
}

第二步、配置自定义主题

依然附上官网地址element-plus修改主题色官网示例

增加了一个预处理样式的配置,以及导入时读取预处理配置

// vite.config.js
import path from "path";
import {
  defineConfig
} from 'vite'
import vue from '@vitejs/plugin-vue'
// 自动引入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {
  ElementPlusResolver
} from 'unplugin-vue-components/resolvers'
// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      // 配置资源路径
      "@/": `${path.resolve(__dirname, "src")}/`,
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        // 自定义的主题色
        additionalData: `@use "@/styles/element/index.scss" as *;`,
      },
    },
  },
  plugins: [
    vue(),
    // 自动引入
    AutoImport({
      resolvers: [ElementPlusResolver({
        // 自动引入修改主题色添加这一行,使用预处理样式,不添加将会导致使用ElMessage,ElNotification等组件时默认的主题色会覆盖自定义的主题色
        importStyle: "sass",
      })],
    }),
    Components({
      resolvers: [ElementPlusResolver({
        // 自动引入修改主题色添加这一行,使用预处理样式
        importStyle: "sass",
      })],
    })
  ],
});

附上我的样式:

// @/styles/element/index.scss
@forward "element-plus/theme-chalk/src/common/var.scss" with (
  $colors: (
    "primary": (
      "base": #3F9A12,
    ),
    "success": (
      "base": #67C23A,
    ),
    "warning": (
      "base": #E6A23C,
    ),
    "danger": (
      "base": #F56C6C,
    ),
    "error": (
      "base": #F56C6C,
    ),
    "info": (
      "base": #909399,
    ),
  ),
  $button-padding-horizontal: (
    "default": 80px
  )
);

直接看结果

我的项目:v3-plus: vue3.x + element-plus项目,使用vite (gitee.com)

在线演示:我的项目

相关文章
element-ui 里 el-popover 位置发生偏移
element-ui 里 el-popover 位置发生偏移
3061 0
|
JavaScript
Vue 报错Failed to mount component: template or render function not defined
Vue 报错Failed to mount component: template or render function not defined
Vue 报错Failed to mount component: template or render function not defined
|
前端开发 JavaScript API
oss大文件上传
oss大文件上传
2535 6
|
运维 算法 应用服务中间件
运维系列.Nginx中使用HTTP压缩功能(一)
运维系列.Nginx中使用HTTP压缩功能(一)
1140 1
|
前端开发 JavaScript
文本,wangEditor5展示HTML无样式,wangEditor5如何看源码,Ctrl + U看CSS文件,代码高亮,Prism.js可以实现,解决方法,参考网页源代码的写法
文本,wangEditor5展示HTML无样式,wangEditor5如何看源码,Ctrl + U看CSS文件,代码高亮,Prism.js可以实现,解决方法,参考网页源代码的写法
|
JavaScript Android开发
|
存储 资源调度 前端开发
搭建Vue3组件库:第十二章 使用 Monorepo 方式管理组件生态
本章介绍采用Monorepo的方式管理组件生态
2075 0
搭建Vue3组件库:第十二章 使用 Monorepo 方式管理组件生态
|
弹性计算 大数据 测试技术
阿里云4核8g服务器价格以及收费标准_2024年新版报价
阿里云服务器4核8g配置多少钱一年?1个月费用多少?云服务器u1实例3折优惠价955.58元一年,计算型c7云服务器4核8G价格2944.79元一年。4核8G服务器按月购买比较贵,经济型e实例4核8G配置1个月216元,通用算力型u1服务器336.96元一个月
|
前端开发 JavaScript 小程序
uniapp中scroll-view局部滚动的各种场景
可滚动视图区域,用于区域滚动。使用竖向滚动时,需要给 scroll-view 一个固定高度,通过 css 设置 height;使用横向滚动时,需要给 scroll-view 添加 white-space: nowrap; 样式
|
Java Linux Maven
使用nexus上传jar包图文教程
使用nexus上传jar包图文教程
3297 0
使用nexus上传jar包图文教程