Element UI custom theme configuration

Yuly 2021-05-03 19:16:20
element ui custom theme configuration

According to official documents , Introduce coordination on demand babel-plugin-component Use , And then .babelrc It is amended as follows :

"presets": [["es2015", { "modules": false }]],
"plugins": [
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
 Copy code 

We can also customize to cover element-ui Of scss Style variables , To change the theme color . establish element-variables.scss

/* Change the theme color variable */
$--color-primary: #912;
/* change icon Font path variable , It's necessary */
$--font-path: '~element-ui/lib/theme-chalk/fonts';
@import "~element-ui/packages/theme-chalk/src/index";
 Copy code 

And then in main.js Import in file element-variables.scss:

import { Button } from 'element-ui'
import '@/assets/style/element-variables.scss'
Vue.component(Button.name, Button)
 Copy code 

But now there's a problem , Modify the theme in this way css A very large . But if you comment out the statement style, it doesn't work , It's very frustrating .

Through the picture below , We can see that in fact babel-plugin-component It's in effect . however app.[hash].css It's a huge volume , It contains element-ui All styles of , Even with chunk-vendors.[hash].css There is a repetition in the style of .


The reason is that babel-plugin-component The default is lib Folders for packaging , But in lib The style file in the folder is css file , So that we can't pass, just modify scss We can change the theme color dynamically by covering the style variable . That's why if we annotate @import "~element-ui/packages/theme-chalk/src/index"; sentence , The reason why custom themes don't work , The style variable we redefined earlier is actually to cover index.scss Variables in the file , Then by regenerating the new style to cover the original style file to achieve the effect of custom theme .

But we can change it babel-plugin-component To configure + Global import style variables to achieve real style on-demand loading .

modify .babelrc file :

presets: [
plugins: [
'libraryName': 'element-ui',
// By default ,libDir The default is lib Folder 
'libDir': 'packages',
// scss The document is placed in packages/theme-chalk/src Under the 
'styleLibraryName': 'theme-chalk/src',
'ext': '.scss'
 Copy code 

Now , What can be introduced on demand is scss 了 , And then we're configuring the global scss Variable . modify element-variables.scss file , Keep only variables :

/* Change the theme color variable */
$--color-primary: #912;
/* change icon Font path variable , It's necessary */
// $--font-path: '~element-ui/lib/theme-chalk/fonts';
// @import "~element-ui/packages/theme-chalk/src/index";
 Copy code 

To configure vue.config.js file :

css: {
loaderOptions: {
scss: {
prependData: [
'@import "src/assets/style/element-variables.scss";',
// Not necessary 
// Introduce here element-ui Its own variables , Because other style files may use its own variables 
// But there is no overlay definition in the custom variable file 
'@import "~element-ui/packages/theme-chalk/src/common/var.scss";'
 Copy code 

stay main.js You don't need to introduce custom theme styles as you did at the beginning :

import { Button } from 'element-ui'
// import '@/assets/style/element-variables.scss'
Vue.component(Button.name, Button)
 Copy code 

Packing effect :



  1. CSS layout
  2. Application scenario explanation of Vue dynamic component
  3. Redux learning notes 04 -- using multiple reducers to manage data
  4. After three months of typescript writing, what have I learned?
  5. Node family - what is a callback?
  6. React -- a simple implementation of render & create element
  7. JS learning simple usage of jquery
  8. Seamless love
  9. 小白前端入门笔记(12),设置哑链接
  10. Small white front-end entry notes (12), set dumb links
  11. Vue2. X opens composition API and TSX
  12. Interview record and thinking of social recruitment for one and a half years (Alibaba, Tencent, baidu offer)
  13. Flex learning notes
  14. The most essential closure article in the eastern hemisphere
  15. 2021-05-03 hot news
  16. Sword finger offer -- reverse order pair in array (JS Implementation)
  17. Working process of scaffold
  18. Use decorator mode to strengthen your fetch
  19. [JS] scope (Introduction)
  20. Employment information statistics network (interface document)
  21. Analysis of MVC
  22. [middle stage] please stay and join me in the backstage
  23. Understanding front end garbage collection
  24. [continuous update] front end special style implementation
  25. Flutter product analysis and package reduction scheme
  26. XPath positioning
  27. 前端开发css中的flex布局的使用
  28. The use of flex layout in front end development CSS
  29. JQuery核心函数和静态方法
  30. JQuery core functions and static methods
  31. Node family - understanding of blocking and non blocking
  32. 热点微前端Microfrontend的讨论:谷歌AdWords是真实的微前端
  33. Vue source code analysis (2) initproxy initialization proxy
  34. What's TM called react diff
  35. Summary of common front end data structure
  36. Useeffect in hooks
  37. [encapsulation 02 design pattern] Command pattern, share meta pattern, combination pattern, proxy pattern, strategy pattern
  38. Front end notes: virtual Dom and diff of vue2. X
  39. The best code scanning plug-in of flutter
  40. The simplest plug-in for rights management of flutter
  41. 21. Object oriented foundation "problems and solutions of object traversal"
  42. Discussion on hot micro front end: Google AdWords is a real micro front end
  43. Usecallback and usememo for real performance optimization
  44. 【前端圭臬】十一:从规范看 JavaScript 执行上下文(下)
  45. [front end standard] 11: Javascript execution context from the perspective of specification (2)
  46. Hexagonal六角形架构ReactJS的实现方式 - Janos Pasztor
  47. Transaction of spring's reactive / imperative relational database
  48. The implementation of hexagonal hexagonal reactjs Janos pasztor
  49. HTTP状态码:402 Payment Required需要付款 - mozilla
  50. HTTP status code: 402 payment required - Mozilla
  51. Factory mode, constructor mode and prototype mode
  52. Build the scaffold of react project from scratch (Series 1: encapsulating a request method with cache function based on Axios)
  53. Cocos Quick Start Guide
  54. Comparison of three default configurations of webpack5 modes
  55. A case study of the combination of flutter WebView and Vue
  56. CSS: BFC and IFC
  57. A common error report and solution in Vue combat
  58. JS: this point
  59. JS: prototype chain
  60. JavaScript series -- promise, generator, async and await