当前位置:   article > 正文

vue3+elementplus动态主题切换_element-plus多主题切换

element-plus多主题切换

1.下载element-plus

  1. # Yarn
  2. $ yarn add element-plus

2.在main.js中引入

  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import ElementPlus from 'element-plus'
  4. import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
  5. const app = createApp(App)
  6. app.use(ElementPlus, {
  7. locale: zhCn,
  8. })
  9. app.mount('#app')

3.在src文件夹下新建style文件夹,并新建文件fix.scss,style.scss

下载yarn add -D sass  

新增fix.scss

在此文件可以自定义基础颜色

  1. /* 覆盖element-plus样式 */
  2. :root {
  3. --el-color-primary: #409EFF;
  4. --el-color-primary-light-1: #53a7ff;
  5. --el-color-primary-light-2: #66b1ff;
  6. --el-color-primary-light-3: #79bbff;
  7. --el-color-primary-light-4: #8cc4ff;
  8. --el-color-primary-light-5: #9fceff;
  9. --el-color-primary-light-6: #b2d8ff;
  10. --el-color-primary-light-7: #c5e1ff;
  11. --el-color-primary-light-8: #d8ebff;
  12. --el-color-primary-light-9: #ebf5ff;
  13. --el-color-primary-dark-1: #398ee5;
  14. --el-color-primary-dark-2: #337ecc;
  15. --el-color-primary-dark-3: #2c6eb2;
  16. --el-color-primary-dark-4: #265e99;
  17. --el-color-primary-dark-5: #204f7f;
  18. --el-color-primary-dark-6: #193f66;
  19. --el-color-primary-dark-7: #132f4c;
  20. --el-color-primary-dark-8: #0c1f32;
  21. --el-color-primary-dark-9: #060f19;
  22. }

 新建style.scss

@import 'fix.scss';

4.配置vite.config.js

下载yarn add -D unplugin-auto-import unplugin-vue-components 5

  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import path from 'path'
  4. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  5. import Components from 'unplugin-vue-components/vite'
  6. import AutoImport from 'unplugin-auto-import/vite'
  7. const pathSrc = path.resolve(__dirname, 'src')
  8. // https://vitejs.dev/config/
  9. export default defineConfig({
  10. plugins: [
  11. vue(),
  12. AutoImport({ resolvers: [ElementPlusResolver()] }),
  13. //Components({ resolvers: [ElementPlusResolver()] }),
  14. // { importStyle: "sass" }配置ElementPlus采用sass样式配色系统
  15. Components({ resolvers: [ElementPlusResolver({ importStyle: "sass" })] }),
  16. ],
  17. resolve: {
  18. alias: {
  19. '~/': `${pathSrc}/`,
  20. },
  21. },
  22. css: {
  23. preprocessorOptions: {
  24. scss: {
  25. additionalData: `@use "~/style/style.scss" as *;`,
  26. },
  27. },
  28. },
  29. })

5.在页面查看效果 

在测试页面写入

  1. <template>
  2. <div>
  3. <el-button type="primary">Primary</el-button>
  4. <div class="demo-color-block">
  5. <span class="demonstration">切换主题</span>
  6. <el-color-picker v-model="color1" :predefine="colorList" @change="changeColor"/>
  7. </div>
  8. </div>
  9. </template>
  10. <script setup>
  11. import { watch, onMounted, ref } from 'vue'
  12. import colorTool from '~/utils/color'
  13. const color1 = ref('#409EFF')
  14. const colorList=ref(['#409EFF', '#009688', '#536dfe', '#ff5c93', '#c62f2f', '#fd726d'])
  15. const changeColor=(val)=>{
  16. console.log(val,'val');
  17. document.documentElement.style.setProperty('--el-color-primary', val);
  18. for (let i = 1; i <= 9; i++) {
  19. document.documentElement.style.setProperty(`--el-color-primary-light-${i}`, colorTool.lighten(val,i/10));
  20. }
  21. for (let i = 1; i <= 9; i++) {
  22. document.documentElement.style.setProperty(`--el-color-primary-dark-${i}`, colorTool.darken(val,i/10));
  23. }
  24. }
  25. </script>
  26. <style scoped>
  27. </style>

页面展示

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/82143
推荐阅读
相关标签
  

闽ICP备14008679号