当前位置:   article > 正文

vue + elementUI实现换肤

vue + elementUI实现换肤

//vue.config.js/

const webpack = require(‘webpack’)

const ThemeColorReplacer = require(‘webpack-theme-color-replacer’)

const forElementUI = require(‘webpack-theme-color-replacer/forElementUI’)

const appConfig = require(‘./config/app-config.js’)

module.exports = {

configureWebpack: {

plugins: [

// 生成仅包含颜色的替换样式(主题色等)

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”>

看我到底变不变色

看我到底变不变色

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

闽ICP备14008679号