elementPlus custom theme

problem background

1. The actual requirement is that the UI will have its own color system, which is definitely different from the default blue style of element. We definitely need to modify it by customizing the theme color instead of using the component's style override to modify it, because that would be too much work.
2. I searched a lot of articles, but the content is quite messy, anyway, it is all kinds of things mixed together. You have to figure it out yourself.
3. In fact, the custom theme is related to the import method of your elementPlus, but many articles do not talk about your own import method at all. Anyway, after modifying the theme color in your project, it will be sent out.

Effect: Change the default blue to green
image.png

1. Completely import custom theme (if you import element-plus completely, see this)

a. Completely import elementPlus
image.png
b. Customize the theme, write an element.scss file in the css under assets
image.png
c. Install scss to parse the scss file

npm i sass -D

d. Import the scss file you wrote in main.ts
1. Note that you need to comment out the css file originally imported into element-plus
2. Import your own style file
image.png

import { createApp } from 'vue'

  import App from './App.vue'
  import ElementPlus from 'element-plus'
  //引入element-plus相关样式
  // import 'element-plus/dist/index.css'
  // 引入覆盖的scss样式
  import './assets/css/element.scss'

  const app = createApp(App)
  app.use(ElementPlus)

  app.mount('#app')

Demo address: https://gitee.com/rui-rui-an/vue3elementstyle

2. Import elementPlus custom theme on demand (if you import on demand, see this)

a. Import elementPlus as needed

image.png

b. Customize the theme, write an element.scss file in the css under assets
image.png
c. Install scss to parse the scss file

npm i sass -D

d. Import the scss file into vue.config.ts
Note that there are two places that need to be configured, both of which are indispensable.

image.png

parse your scss file

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

When importing on demand, the sass file of the component is loaded, so that it can be overwritten by the scss you wrote

 Components({
      resolvers: [ElementPlusResolver({ importStyle: "sass" })],
    }),

Demo address: https://gitee.com/rui-rui-an/vue3elementstyle2

3. Manually import element-Plus custom theme

a. Install manually imported packages

npm i unplugin-vue-components unplugin-element-plus -D

b. Configure in vue.config.ts

import ElementPlus from 'unplugin-element-plus/vite'
plugins: [
  vue(),
  ElementPlus({
    
     useSource: true,})
  // ...
],

image.png
c. Manually import components and register
image.png
c. Customize the theme, write an element.scss file in css under assets
image.png
d. Introduce the scss file in vue.config.ts

plugins: [
    vue(),
    ElementPlus({
    
     useSource: true,})
    // ...
  ],
css: {
    
    
    preprocessorOptions: {
    
    
      scss: {
    
    
        additionalData: `@use "@/assets/css/element.scss" as *;`,
      },
    },
  },

image.png

Demo address: https://gitee.com/rui-rui-an/vue3elementstyle3

How can there be any love or dislike of writing articles, it's just a stupid brain

Guess you like

Origin blog.csdn.net/weixin_43239880/article/details/132183924