new ThemeColorReplacer({
fileName: ‘style/theme-colors.[contenthash:8].css’,
matchColors: [
…forElementUI.getElementUISeries(appConfig.themeColor)
],
changeSelector: forElementUI.changeSelector,
isJsUgly: process.env.NODE_ENV === ‘production’ ? true : undefined,
injectCss: true // 解决router为history模式下改变样式后刷新页面不生效的问题
})
]
}
}
如果是cli2的话,这个文件中的plugins定义在build/webpack.base.conf.js中。
//src/utils/themeColorClient.js/
import client from ‘webpack-theme-color-replacer/client’
import forElementUI from ‘webpack-theme-color-replacer/forElementUI’
// 注意自己项目里的引入路径
import appConfig from ‘…/…/config/app-config’
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)
}
}
//src/main.js/
import Vue from “vue”;
import App from “./App.vue”;
import router from “./router”;
import store from “./store”;
Vue.config.productionTip = false;
import ElementUI from ‘element-ui’;
Vue.use(ElementUI)
// 主题换肤
import { initThemeColor } from ‘./utils/themeColorClient’
initThemeColor();
new Vue({
router,
store,
render: h => h(App)
}).$mount(“#app”);
//src/views/Home.vue/
主要按钮
<el-color-picker
size=“medium”
@change=“changeColor”>
看我到底变不变色
看我到底变不变色