起因
最近因公司需求,需要實現主題換膚功能,不僅僅是顏色的更改,還需要包括圖片,字體等文件等更換,因此在百度里各種實現方案后,決定根據scss+style-loader/useable做換膚。
項目開始
首先我們用vue-element-admin這個開源的后台管理系統項目來做demo演示,為了便於做二次開發,下載vue-admin-template來開發。
// 從github下載vue-admin-template
clone https://github.com/PanJiaChen/vue-admin-template.git
cd vue-admin-template
npm install
npm run dev
運行成功后的效果

安裝style-loader處理器
因為vue-admin-template項目是添加過sass-loader依賴的,所以不用我們再次安裝,在上一步就已經安裝好了。
npm install style-loader --save-dev
創建主題文件
- 在src目錄下創建theme-chalk、theme-light主題文件夾
- 在兩個主題目錄下創建index.useable.scss文件

- 在index.useable.scss中寫入樣式
// theme-chalk/index.useable.scss
body {
background: red;
}
// theme-light/index.useable.scss
body {
background: green;
}

到此,我們的主題樣式都已經建好了,現在要將主題應用上去
在vue.config.js中增加style-loader/useable
vue-cli2根vue-cli3的配置區別還是挺大的,我在配置的過程中遇到很多坑,因為vue-cli3沒有了webpack.config.js文件,我們在配置webpack的時候無法根據老文檔來配置,需要熟悉cli3的webpack-chain來鏈式修改配置,但是文檔很少,配置的過程中異常困難。
在配置文件中chainWebpack鏈式修改webpack配置就能成功應用loader了,話不多說,直接上代碼,
// 換膚loader
const scss = config.module.rule('scss').toConfig();
const useable = { ...scss.oneOf[3], test: /\.useable.scss$/ };
useable.use = [...useable.use];
useable.use[0] = { loader: 'style-loader/useable' };
config.module.rule('scss').merge({ oneOf: [useable] });

使用主題
在准備工作都做好后,接下來我們開始使用主題樣式。
之前說的為什么要用style-loader來做換膚呢?是因為style-loader提供了useable這一API,可動態加載刪除link文件。具體詳情請自行去看看style-loader。
在src目錄下,創建theme.js文件
const cache = {};
const themeAction = {
chalk() {
if (!cache.chalk) {
cache.chalk = import('./styles/theme-chalk/index.useable.scss');
}
return cache.chalk;
},
light() {
if (!cache.light) {
cache.light = import('./styles/theme-light/index.useable.scss');
}
return cache.light;
}
};
let current = null;
async function setTheme(theme) {
if (themeAction[theme]) {
const style = await themeAction[theme]();
if (current) {
current.unref();
}
style.ref();
current = style;
}
}
window.setTheme = setTheme;
export default setTheme;
在main.js中,引入theme.js。
import setTheme from './theme'
// 使用主題
setTheme('chalk')
重啟服務,查看效果

在實際項目中,可根據傳入的主題(chalk/light),動態切換主題色,同時也可根據業務需求,創建多個主題。我們只需要在index.useable.scss文件中寫樣式變量即可。