温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用vue怎么实现主题切换

发布时间:2021-06-01 16:08:33 来源:亿速云 阅读:131 作者:Leah 栏目:开发技术

这篇文章给大家介绍使用vue怎么实现主题切换,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

第一种办法:动态组件

当主题的路由并没有发生变化,仅是组件内部的样式,功能发生了变化,我们可以将一个组件复制一遍,修改完后,通过懒加载和动态组件实现。

// 页面组件
<template>
    <div>
        <component :is="themeName" />
    </div>
</template>
<script>
    export default{
        name: 'Home',
        components:{
            theme1:()=>import('@/theme/theme1/a'),
            theme2:()=>import('@/theme/theme2/a'),
        },
        computed:{
            themeName(){
                retun `theme${this.$store.state.themeId}`
            }
        }
    }
</script>

在组件中,我将script部分抽离出来,因为大部分组件其实在逻辑上是相同。哪怕有一些不同,我们也可以直接在主题2的组件中更改,减少对主题1的影响。

//action.js
export default{
    name:'Theme1',
    ....
}
<template>
<div class="theme1"></div>
</template>
<script>
    import action from '../componentAction/action'
    action.name='Theme1'
    export default action
</script>
<style scoped>

</style>

这样实现的有点是可以通过子组件的style scoped实现样式隔离,同时功能数据上都会隔离,例如两个子组件中的swiper不会相互影响。 同时,懒加载也减小了首页的加载时体积。 后面再增加新增的主题也只是照猫画虎而已。

第二种办法,路由隔离

路由隔离其实就是简单的theme1写一个路由的数组,theme2写一套路由。

// router.js
{
    path:'/theme3',
    name:'theme3Index',
    component: () => import('../views/theme3/Index.vue'),
    children:[
      {
        path: '/theme3/entry',
        name: 'theme3Entry',
        component:  () => import('../views/theme3/entry.vue'),
      }
     ]
 }

这种办法其实是下下之策,我使用这个主要是因为路由变化了,比如之前是直接进入a.vue,但是现在前面多加了一层entry页面,所以只能改变路由。 这种办法也实现了比较好的隔离。

总结

以上两种思路是我针对于我们当前业务的思考,仅供参考。

其实这两种方法都有一个共同的问题,就是代码冗余。每个组件都避不可免的带有一部分之前主题的代码,虽然,大部分逻辑代码可以抽离出来,但是css和template却无法抽离。

如果每次一个主题增加一个dom,一个功能块,如果每次都用v-if,那么其实代码以后会更加难以维护。因此,我选择了按照主题去划分代码。

额外补充基于css的两种方法

方法一 多套css

<!-- 中心 -->
<template>
 动态获取父级class名称,进行一个父级class的多次定义
  <div :class="className">
    <div class="switch" v-on:click="chang()">
      {{ className == "box" ? "开灯" : "关灯" }}
    </div>
  </div>
</template>
<script>
export default {
  name: "Centre",
  data() {
    return {
      className: "box"
    };
  },
  methods: {
  // 改变class
    chang() {
      this.className === "box"
        ? (this.className = "boxs") 
        : (this.className = "box");
    }
  },
};
</script>
<style lang="scss">
当class为box 使用witch的css
@import "./style/witch.scss";
当class为boxs 使用black的css
@import "./style/black.scss";
.switch {
  position: fixed;
  top: 4px;
  right: 10px;
  z-index: 50;
  width: 60px;
  height: 60px;
  background: #fff;
  line-height: 60px;
  border-radius: 20%;
}
</style>

每个css文件样式大致相同,只是最外层的父级不一样,分别为.box 和.boxs

方法二 scss动态切换变量

我自己是分为了2个主要文件来做的

  • _variable.scss 变量管理文件

  • var()为css3中提出的声明样式变量的方法

  • var(属性名,属性值)注意属性值不能是字符串

// 主题切换
$bgColor:var(--backgroundColor,rgb(255,255,255));
$fontColor:var(--fonntColor,rgb(0,0,0));
$bgmColor:var(--backgroundMColor,rgb(238,238,238));
$tableColor:var(--tableColor,rgb(218,218,218));
$borderColor:var(--borderColor,rgb(238,238,238));
$tablesColor:var(--tablesColor,rgb(255,255,255));
$inputColor:var(--inputColor,rgb(255,255,255))

创建的_variable.scss 文件我在vue.config.js进行了一个全局的配置,没有在组件中引入

  css: {
    loaderOptions: {
      // 此文件为主题切换文件
      sass: {
        prependData: `@import "./src/styles/_variable.scss";`,
      },
    },
  },

publicStyle.js

这个方法可以去修改var定义的变量
document.getElementsByTagName("body")[0].style.setProperty("属性名", "替换的属性值f");

//  主题切换
const cut = (cutcheack) => {
    document.getElementsByTagName("body")[0].style.setProperty("--backgroundColor", cutcheack ? "#121212" : "#fff");
    document.getElementsByTagName("body")[0].style.setProperty("--fonntColor", cutcheack ? "#cecece" : "#333");
    document.getElementsByTagName("body")[0].style.setProperty("--backgroundMColor", cutcheack ? "#333" : "#eee");
    document.getElementsByTagName("body")[0].style.setProperty("--tableColor", cutcheack ? "#000" : "#d8d8d8");
    document.getElementsByTagName("body")[0].style.setProperty("--tablesColor", cutcheack ? "#222" : "#fff");
    document.getElementsByTagName("body")[0].style.setProperty("--inputColor", cutcheack ? "#666" : "#fff");
    document.getElementsByTagName("body")[0].style.setProperty("--borderColor", cutcheack ? "#666" : "#fff");
};
export default cut;

组件中使用

<!-- 首页 -->
<template>
<div class='home'>
      <el-switch v-model="cutcheack" active-color="#333" inactive-color="#13ce66"  active-text="主题" @change="switchs"></el-switch>
</div>
</template>
<script>
import cut from "../../utils/publicStyle.js";
export default {
  name: "Home",
  data() {
    return {
      cutcheack: false, //主题切换
    };
  },
  methods: {
    // 左侧导航隐藏或显示
    // 切换主题
    switchs() {
      cut(this.cutcheack);
    },
  },
};
</script>
<style lang='scss' scope>
.home {
    height: 100%;
    width: 100%;
	background:$bgColor;
    .el-container {
        height: 100%;
        color:$fontColor;
    }
}
</style>

关于使用vue怎么实现主题切换就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue
AI