vue實(shí)戰(zhàn)-實(shí)現(xiàn)換主題/皮膚功能

現(xiàn)在的app和pc網(wǎng)站做的越來(lái)越花哨鸽心,但是有時(shí)候用戶(hù)并不喜歡你給他挑選好的主題顏色瞒滴,這個(gè)時(shí)候就需要一個(gè)換皮膚的功能了姨夹。

那么我們?cè)趺丛趘ue中實(shí)現(xiàn)這個(gè)換皮膚的功能呢夸溶?

項(xiàng)目結(jié)構(gòu)

  1. 實(shí)現(xiàn)思路

    • 我們用vue一般都是寫(xiě)單頁(yè)面程序吴旋,因此在實(shí)際發(fā)布的時(shí)候只有一個(gè)html以及一堆靜態(tài)文件(js剧辐、css、img之類(lèi))邮府。而在html中引用了這些js和css荧关。我們要換皮膚的話其實(shí)就是動(dòng)態(tài)的去切換css,所以在這里實(shí)現(xiàn)換皮膚其實(shí)也就是動(dòng)態(tài)的更改html中引用css的路徑褂傀,使得當(dāng)用戶(hù)選擇了不同的皮膚忍啤,頁(yè)面引用的css不同從而呈現(xiàn)的樣式也不一樣。
  2. 優(yōu)化策略

    • 其實(shí)在實(shí)際場(chǎng)景中仙辟,需要通過(guò)切換皮膚來(lái)改變css的元素占所有css的比重并不會(huì)很多同波,因此我們需要把需要通過(guò)切換改變的css單獨(dú)提取出來(lái),在動(dòng)態(tài)改變css路徑時(shí)只需要去改變這個(gè)控制皮膚的css就可以了叠国。
    • 把皮膚相關(guān)的css壓縮未檩。
  3. 實(shí)現(xiàn)代碼分析
    如下是我們的html代碼,注意其中的<link rel="stylesheet" name="theme" href="">粟焊,其他的都是正常引用冤狡。

<!DOCTYPE html>
<html lang="zh-CN">
<head>
   <title>iView admin</title>
   <meta charset="UTF-8">
   <!--  -->
   <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0">
   <!-- 引入的css -->
   <link rel="stylesheet" href="/dist/main.css">
   <!-- 注意這是我們換皮膚需要的css -->
   <link rel="stylesheet" name="theme" href="">
   <!-- 圖標(biāo) -->
   <link rel="icon" href="./td_icon.ico" type="image/x-icon"/>
</head>
<body>
<div id="app"></div>
<!-- 用到的js -->
<script type="text/javascript" src="/dist/vender-base.js"></script>
<script type="text/javascript" src="/dist/vender-exten.js"></script>
<script type="text/javascript" src="/dist/main.js"></script>
</body>
</html>

接下來(lái)就是具體實(shí)現(xiàn)換皮膚功能了,換皮膚一般都是點(diǎn)擊一個(gè)按鈕彈出一些皮膚的選項(xiàng)项棠,選中選項(xiàng)后皮膚生效悲雳。
我們將換皮膚功能抽成一個(gè)組件theme-switch。pc端使用iview香追,手機(jī)端使用了vant合瓢。一共有3套皮膚用于切換。


目錄結(jié)構(gòu)
  • pc端
<template>
    <div style="display:inline-block;padding:0 6px;">
        <Dropdown trigger="click" @on-click="setTheme">
            <a href="javascript:void(0)">
                <Icon :style="{marginTop: '-2px', verticalAlign: 'middle'}" color="#495060" :size="18" type="paintbucket"></Icon>
                <Icon type="arrow-down-b"></Icon>
            </a>
            <DropdownMenu slot="list">
                <DropdownItem v-for="(item, index) in themeList" :key="index" :name="item.name">
                    <Row type="flex" justify="center" align="middle">
                        <span style="margin-right:10px;"><Icon :size="20" :type="item.name.substr(0, 1) !== 'b' ? 'happy-outline' : 'happy'" :color="item.menu"/></span>
                        <span><Icon :size="22" type="record" :color="item.element"/></span>
                    </Row>
                </DropdownItem>
            </DropdownMenu>
        </Dropdown>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import config from '../../../../build/config.js';
export default {
    name: 'themeSwitch',
    data () {
        return {
            themeList: [
                {
                    name: 'black_b',
                    menu: '#495060',
                    element: '#2d8cf0'
                },
                {
                    name: 'black_g',
                    menu: '#495060',
                    element: '#00a854'
                },
                {
                    name: 'black_y',
                    menu: '#495060',
                    element: '#e96500'
                }
            ]
        };
    },
    methods: {
        // 點(diǎn)擊切換事件
        setTheme (themeFile) {
            let menuTheme = themeFile.substr(0, 1);
            let mainTheme = themeFile.substr(-1, 1);
            if (menuTheme === 'b') {
                // 黑色菜單
                this.$store.commit('changeMenuTheme', 'dark');
                menuTheme = 'dark';
            } else {
                this.$store.commit('changeMenuTheme', 'light');
                menuTheme = 'light';
            }
            let path = '';
               // 取到我們?cè)趆tml上給皮膚的css留的坑并且設(shè)置路徑
            let themeLink = document.querySelector('link[name="theme"]');
            let userName = Cookies.get('user');
            if (localStorage.theme) {
                let themeList = JSON.parse(localStorage.theme);
                let index = 0;
                let hasThisUser = themeList.some((item, i) => {
                    if (item.userName === userName) {
                        index = i;
                        return true;
                    } else {
                        return false;
                    }
                });
                if (hasThisUser) {
                    themeList[index].mainTheme = mainTheme;
                    themeList[index].menuTheme = menuTheme;
                } else {
                    themeList.push({
                        userName: userName,
                        mainTheme: mainTheme,
                        menuTheme: menuTheme
                    });
                }
                localStorage.theme = JSON.stringify(themeList);
            } else {
                localStorage.theme = JSON.stringify([{
                    userName: userName,
                    mainTheme: mainTheme,
                    menuTheme: menuTheme
                }]);
            }
            let stylePath = '';
            if (config.env.indexOf('dev') > -1) {
                stylePath = './src/views/main-components/theme-switch/theme/';
            } else {
                stylePath = 'dist/';
            }
            if (mainTheme !== 'b') {
                path = stylePath + mainTheme + '.css';
            } else {
                path = '';
            }
            themeLink.setAttribute('href', path);
        }
    },
    created () {
        let path = '';
        // 判斷運(yùn)行環(huán)境用于切換地址
        if (config.env.indexOf('dev') > -1) {
            path = './src/views/main-components/theme-switch/theme/';
        } else {
            path = 'dist/';
        }
        let name = Cookies.get('user');
        // 如果用戶(hù)之前選擇過(guò)皮膚則直接使用之前選擇的透典,否則使用默認(rèn)皮膚
        if (localStorage.theme) {
            let hasThisUser = JSON.parse(localStorage.theme).some(item => {
                if (item.userName === name) {
                    this.$store.commit('changeMenuTheme', item.menuTheme);
                    this.$store.commit('changeMainTheme', item.mainTheme);
                    return true;
                } else {
                    return false;
                }
            });
            if (!hasThisUser) {
                this.$store.commit('changeMenuTheme', 'dark');
                this.$store.commit('changeMainTheme', 'b');
            }
        } else {
            this.$store.commit('changeMenuTheme', 'dark');
            this.$store.commit('changeMainTheme', 'b');
        }
        // 根據(jù)用戶(hù)設(shè)置主題
        if (this.$store.state.app.themeColor !== 'b') {
            let stylesheetPath = path + this.$store.state.app.themeColor + '.css';
            // 取到我們?cè)趆tml上給皮膚的css留的坑并且設(shè)置路徑
            let themeLink = document.querySelector('link[name="theme"]');
            themeLink.setAttribute('href', stylesheetPath);
        }
    }
};
</script>
  • 手機(jī)端
<template>
  <div style="display:inline-block;padding:0 6px;">
    <div @click="showBtn">換皮膚</div>
    <van-actionsheet v-model="show" :actions="themeList" @select="setTheme"/>
  </div>
</template>

<script>
import Cookies from "js-cookie";

import { Actionsheet } from "vant";
// import config from "../../../../build/config.js";
export default {
  name: "themeSwitch",
  components: {
    [Actionsheet.name]: Actionsheet
  },
  data() {
    return {
      show: false,
      themeList: [
        {
          name: "黑色",
          data: "black_b"
        },
        {
          name: "綠色",
          data: "black_g"
        },
        {
          name: "黃色",
          data: "black_y"
        }
      ]
    };
  },
  methods: {
    showBtn() {
      this.show = true;
    },
    setTheme(themeFile) {
      themeFile = themeFile.data;
      let menuTheme = themeFile.substr(0, 1);
      let mainTheme = themeFile.substr(-1, 1);
      if (menuTheme === "b") {
        // 黑色菜單
        this.$store.commit("changeMenuTheme", "dark");
        menuTheme = "dark";
      } else {
        this.$store.commit("changeMenuTheme", "light");
        menuTheme = "light";
      }
      let path = "";
      let themeLink = document.querySelector('link[name="theme"]');
      let userName = Cookies.get("user");
      if (localStorage.theme) {
        let themeList = JSON.parse(localStorage.theme);
        let index = 0;
        let hasThisUser = themeList.some((item, i) => {
          if (item.userName === userName) {
            index = i;
            return true;
          } else {
            return false;
          }
        });
        if (hasThisUser) {
          themeList[index].mainTheme = mainTheme;
          themeList[index].menuTheme = menuTheme;
        } else {
          themeList.push({
            userName: userName,
            mainTheme: mainTheme,
            menuTheme: menuTheme
          });
        }
        localStorage.theme = JSON.stringify(themeList);
      } else {
        localStorage.theme = JSON.stringify([
          {
            userName: userName,
            mainTheme: mainTheme,
            menuTheme: menuTheme
          }
        ]);
      }
      let stylePath = './';
    //   stylePath = "./src/view/component/theme-switch/theme/";
      // if (config.env.indexOf('dev') > -1) {
      //     stylePath = 'src/view/component/theme-switch/theme';
      // } else {
      //     stylePath = 'dist/';
      // }
      if (mainTheme !== "b") {
        path = stylePath + mainTheme + ".css";
      } else {
        path = "";
      }
      themeLink.setAttribute("href", path);
      this.show = false;
    }
  },
  created() {
    let path = "";
    path = "css/";
    // if (config.env.indexOf("dev") > -1) {
    //   path = "src/view/component/theme-switch/theme";
    // } else {
    //   path = "dist/";
    // }

    let name = Cookies.get("user");
    if (localStorage.theme) {
      let hasThisUser = JSON.parse(localStorage.theme).some(item => {
        if (item.userName === name) {
          this.$store.commit("changeMenuTheme", item.menuTheme);
          this.$store.commit("changeMainTheme", item.mainTheme);
          return true;
        } else {
          return false;
        }
      });
      if (!hasThisUser) {
        this.$store.commit("changeMenuTheme", "dark");
        this.$store.commit("changeMainTheme", "b");
      }
    } else {
      this.$store.commit("changeMenuTheme", "dark");
      this.$store.commit("changeMainTheme", "b");
    }
    console.log(path);
    // 根據(jù)用戶(hù)設(shè)置主題
    if (this.$store.state.app.themeColor !== "b") {
      let stylesheetPath = path + this.$store.state.app.themeColor + ".css";
      let themeLink = document.querySelector('link[name="theme"]');

      themeLink.setAttribute("href", stylesheetPath);
    }
  }
};
</script>

在首頁(yè)引用該組件晴楔,初次渲染時(shí)進(jìn)入該組件的creat方法,如果用戶(hù)之前選擇過(guò)皮膚則直接使用之前選擇的峭咒,否則使用默認(rèn)皮膚税弃。在store中加入相應(yīng)方法。

changeMenuTheme (state, theme) {
    state.menuTheme = theme;
},
changeMainTheme (state, mainTheme) {
     state.themeColor = mainTheme;
 }

動(dòng)態(tài)切換最關(guān)鍵的是這兩行代碼:
let themeLink = document.querySelector('link[name="theme"]')
themeLink.setAttribute('href', stylesheetPath)
但是這個(gè)時(shí)候我們皮膚相關(guān)的css并沒(méi)有打到代碼中讹语,需要我們額外進(jìn)行配置钙皮。
在webpack的配置文件中找到plugins,加入以下插件:

  • pc端
 new CopyWebpackPlugin([
            {
                from: 'td_icon.ico'
            },
            {
                from: 'src/styles/fonts',
                to: 'fonts'
            },
            {
                from: 'src/views/main-components/theme-switch/theme'
            }
        ],
  • 手機(jī)端
 new CopyWebpackPlugin([
            {
                from: 'static',
                to: 'static'
            },
            {
                from: 'src/view/component/theme-switch/theme',
                to: './css'
            }
        ])

之前我們可能已經(jīng)有了這個(gè)插件了,現(xiàn)在只是需要把皮膚相關(guān)的css額外配置一下短条。以上工作完成之后已經(jīng)可以動(dòng)態(tài)的切換html中皮膚相關(guān)的css路徑了导匣。接下來(lái)就需要我們?cè)谛枰袚Qcss的地方引用具體的class并且寫(xiě)三套樣式分別放在theme中的css文件里。

注意在具體的vue文件中不需要引用theme中的css茸时,因?yàn)閔tml中已經(jīng)幫我們引用了

如果報(bào)各種與路徑有關(guān)的錯(cuò)誤那就是你的路徑真的寫(xiě)錯(cuò)啦贡定。好好對(duì)比一下組件中引用的路徑,webpack中配置的路徑和你的項(xiàng)目路徑吧~

當(dāng)然這只是換皮膚的一種實(shí)現(xiàn)思路可都,也就是動(dòng)態(tài)切換html中的引用路徑缓待。也希望大家集思廣益提供更多的解決思路~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市渠牲,隨后出現(xiàn)的幾起案子旋炒,更是在濱河造成了極大的恐慌,老刑警劉巖签杈,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瘫镇,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡答姥,警方通過(guò)查閱死者的電腦和手機(jī)铣除,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)鹦付,“玉大人尚粘,你說(shuō)我怎么就攤上這事∏贸ぃ” “怎么了郎嫁?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)潘明。 經(jīng)常有香客問(wèn)我行剂,道長(zhǎng),這世上最難降的妖魔是什么钳降? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮腌巾,結(jié)果婚禮上遂填,老公的妹妹穿的比我還像新娘。我一直安慰自己澈蝙,他們只是感情好吓坚,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著灯荧,像睡著了一般礁击。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,190評(píng)論 1 299
  • 那天哆窿,我揣著相機(jī)與錄音链烈,去河邊找鬼。 笑死挚躯,一個(gè)胖子當(dāng)著我的面吹牛强衡,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播码荔,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼漩勤,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了缩搅?” 一聲冷哼從身側(cè)響起越败,我...
    開(kāi)封第一講書(shū)人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎硼瓣,沒(méi)想到半個(gè)月后究飞,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡巨双,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年噪猾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片筑累。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡袱蜡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出慢宗,到底是詐尸還是另有隱情坪蚁,我是刑警寧澤,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布镜沽,位于F島的核電站敏晤,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏缅茉。R本人自食惡果不足惜嘴脾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蔬墩。 院中可真熱鬧译打,春花似錦、人聲如沸拇颅。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)樟插。三九已至韵洋,卻和暖如春竿刁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背搪缨。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工食拜, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人勉吻。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓监婶,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親齿桃。 傳聞我的和親對(duì)象是個(gè)殘疾皇子惑惶,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)短纵。 注意:講述HT...
    kismetajun閱讀 27,474評(píng)論 1 45
  • 問(wèn)答題47 /72 常見(jiàn)瀏覽器兼容性問(wèn)題與解決方案带污? 參考答案 (1)瀏覽器兼容問(wèn)題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 13,748評(píng)論 1 92
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件香到、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,094評(píng)論 4 62
  • 昨天拖著疲憊的身體鱼冀,陪兒子走進(jìn)了全新的校園,開(kāi)啟了他最美好的高中生活悠就。大雨小雨下了一天千绪,可心情是好的,除了身體疲憊...
    尋一束光閱讀 277評(píng)論 0 0
  • 在我很小的時(shí)候梗脾,我的媽媽對(duì)我是很好的荸型,她喜歡給我買(mǎi)衣服,喜歡給我扎辮子炸茧。她逛街只要看見(jiàn)漂亮的小衣服小裙子瑞妇,都會(huì)買(mǎi)回...
    大碼微拍閱讀 85評(píng)論 0 0