我是靠谱客的博主 忧虑蛋挞,这篇文章主要介绍vue在线动态切换主题色方案,现在分享给大家,希望可以做个参考。

主要原理是利用webpack插件webpack-theme-color-replacer提取相关颜色css然后根据配置动态生成替换的css

具体实现步骤如下:

1.添加webpack插件,新建文件webpack/themePlugin.js

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const ThemeColorReplacer = require('webpack-theme-color-replacer') const forElementUI = require('webpack-theme-color-replacer/forElementUI') const config = require('../src/config/appConfig') module.exports = new ThemeColorReplacer({ fileName: 'css/theme-colors.[contenthash:8].css', matchColors: [ ...forElementUI.getElementUISeries(config.themeColor), //element-ui主色系列 // '#0cdd3a', //自定义颜色 ], changeSelector: forElementUI.changeSelector, isJsUgly: process.env.NODE_ENV !== 'development', // injectCss: false, // resolveCss(resultCss) { // optional. Resolve result css code as you wish. // return resultCss + youCssCode // } })

matchColors数组中可配置多个自定义要替换的主题色

2.在vue.config.js中添加这个插件

复制代码
1
2
3
4
5
6
7
8
const themePlugin=require('./webpack/themePlugin'); module.exports = { configureWebpack: { plugins: [ themePlugin ] } }

3.新建文件themeColorClient.js

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import client from 'webpack-theme-color-replacer/client' import forElementUI from 'webpack-theme-color-replacer/forElementUI' import appConfig from '@/config/appConfig' export let curColor = appConfig.themeColor // 动态切换主题色 export function changeThemeColor(newColor) { var options = { newColors: [...forElementUI.getElementUISeries(newColor)], } return client.changer.changeColor(options, Promise) .then(() => { curColor = newColor localStorage.setItem('theme_color', curColor) }); } export function initThemeColor() { const savedColor = localStorage.getItem('theme_color') if (savedColor) { curColor = savedColor changeThemeColor(savedColor) } }

4.在需要的时候调用 initThemeColor初始化颜色 changeThemeColor改变主题颜色

复制代码
1
2
3
import { initThemeColor,changeThemeColor } from './utils/themeColorClient' initThemeColor() changeThemeColor('#F56C6C')//传入颜色格式应该为十六进制颜色值,'red'类似颜色暂不支持

具体细节请参考https://github.com/hzsrc/vue-element-ui-scaffold-webpack4

到此这篇关于vue在线动态切换主题色方案的文章就介绍到这了,更多相关vue 动态切换主题内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是忧虑蛋挞最近收集整理的关于vue在线动态切换主题色方案的全部内容,更多相关vue在线动态切换主题色方案内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(122)

评论列表共有 0 条评论

立即
投稿
返回
顶部