首页 > 其他分享 >Vue中 使用 Scss 实现配置、切换主题

Vue中 使用 Scss 实现配置、切换主题

时间:2023-10-25 16:00:49浏览次数:35  
标签:Scss scss Vue assets color theme 切换 include icon


1. 样式文件目录介绍

Vue中 使用 Scss 实现配置、切换主题_scss

本项目中的公共样式文件均位于 src/assets/css 目录下,其中 index.scss是总的样式文件的汇总入口 ,common.scss 是供全局使用的一些基本样式(常量), _theme.scss、_handle.scss 两个文件是进行主题颜色配置的文件。

如下图,将 index.scss 在 main.js 文件中引入即可全局使用。.

Vue中 使用 Scss 实现配置、切换主题_App_02

2. 主题 scss 文件配置

src/assets/css 目录下的 _themes.scss,里面可配置不同的主题配色方案,本文配置了两个主题颜色:light、dark。

@import './common.scss';
$themes: (
  light: (
    bg-color: $white,
    font-color: $regularBlack,
    link-color: $grey,
    icon-home: url('~@/assets/img/icon/lightHomeIcon.svg'),
    icon-filter: url('~@/assets/img/icon/lightFilter.png'),
  ),
  dark: (
    bg-color: $black,
    font-color: $white,
    link-color: $blue,
    icon-home: url('~@/assets/img/icon/darkHomeIcon.svg'),
    icon-filter: url('~@/assets/img/icon/darkFilter.png'),
  )
)

src/assets/css 目录下的 _handle.scss 用来操作上述 _themes.scss 中 $theme 的变量,_handle.scss 文件内容:

@import "./_themes.scss";

// 从主题色map中取出对应颜色
@function themed($key) {
  @return map-get($theme-map, $key);
}

// 切换主题时 获取不同的主题色值
@mixin themeify {
  @each $theme-name,$theme-map in $themes {
    // !global 把局部变量强升为全局变量
    $theme-map: $theme-map !global;
    // 判断html的data-theme的属性值  #{}是sass的插值表达式
    // & sass嵌套里的父容器标识   @content是混合器插槽,像vue的slot
    [data-theme="#{$theme-name}"] & {
      @content;
    }
  }
}


// 获取背景颜色
@mixin background_color($color) {
  @include themeify {
    background-color: themed($color) !important;
  }
}

// 获取背景图片
@mixin background_image($color) {
  @include themeify {
    background-image: themed($color) !important;
  }
}

// 获取图片
@mixin content($color) {
  @include themeify {
    content: themed($color) !important;
  }
}

// 获取字体颜色
@mixin font_color($color) {
  @include themeify {
    color: themed($color) !important;
  }
}

// 获取边框颜色
@mixin border_color($color) {
  @include themeify {
    border-color: themed($color) !important;
  }
}

3. 组件中使用

样式文件都配置完成了,怎么设置当前需要使用的主题呢 ?

  • 此处具体情况具体分析,在合适的页面或位置写入即可,本文是写在了 App.vue 项目入口文件中,通过
window.document.documentElement.setAttribute();

方法传入当前想要使用的主题。本文默认传入的 ‘light’,则 vue 页面中使用的即为 _theme.scss 中的 light 对象下配置好的颜色或者其他属性;

  • 设置其他主题色(如:dark、blue)同理,前提是 _theme.scss 文件中存在配置好的对应主题样式;
// App.vue
<template>
  <div id="app">
    <div class="fun">
      <el-switch
        v-model="switchVal"
        active-color="#2c2c2c"
        inactive-color="#e8e4e4"
        @change="switchChange">
      </el-switch>
    </div>
    <el-menu 
      :default-active="activeIndex" 
      mode="horizontal"
      text-color="#fff"
      background-color="#545c64"
      active-text-color="#ffd04b" 
      @select="handleSelect">
      <el-menu-item index="/home">
        主页
      </el-menu-item>
      <el-submenu index="1">
        <template slot="title">图表</template>
        <el-menu-item index="/charts">折线图</el-menu-item>
      </el-submenu>
      <el-submenu index="2">
        <template slot="title">表格</template>
        <el-menu-item index="/table">普通表格</el-menu-item>
        <el-menu-item index="/dynamicTable">动态表格</el-menu-item>
      </el-submenu>
    </el-menu>

    <router-view/>
  </div>
</template>

<script>
export default {
  data(){
    return {
      switchVal: false,
      activeIndex: '/home',
    }
  },
  methods:{
    switchChange(val){
      if(val){
        window.document.documentElement.setAttribute('data-theme', "dark");
      }else{
        window.document.documentElement.setAttribute('data-theme', "light");
      }
    },
    handleSelect(key, keyPath) {
      this.$router.push(key)
    }
  },
  mounted(){
    this.switchChange(this.switchVal);
  }
}
</script>

<style lang="scss">
// 引入主题配置文件
@import "@/assets/css/_handle.scss";
#app {
  height: 100vh;
  text-align: center;
  @include background_color('bg-color');
  // background_color 为 _handle.scss 文件中配置好的属性,传入'bg-color'即可通过当前的主题配置在 _theme.scss 文件中取色。 
  .fun{
    width: 100%;
    display: flex;
    justify-content: flex-end;
    padding: 5px;
    box-sizing: border-box;
  }
}
</style>

home.vue 中同理

<style lang="scss" scoped>
@import "@/assets/css/_handle.scss";
.home{
  text-align: center;
  @include font_color('font-color');
  .homeIcon{
    width: 14px;
    height: 14px;
    margin-right: 5px;
    display: inline-block;
    background-size: 100% 100%;
    @include background_image('icon-home');
  }
  a{
    @include font_color('link-color');
  }
}
</style>

4. 效果

Vue中 使用 Scss 实现配置、切换主题_vue.js_03


标签:Scss,scss,Vue,assets,color,theme,切换,include,icon
From: https://blog.51cto.com/zhengys/7978726

相关文章

  • vue实现动态展开与折叠内联块元素
    功能需求当多个内联块元素(比如div)在一个固定宽度的父元素内自动排列换行时,如果这些元素的行数超过四行,那么默认折叠超出的部分,并在第四行末尾显示一个按钮供用户切换展开或折叠状态。在折叠状态下,为了确保展开/折叠按钮能够显示在第四行末尾,会隐藏第四行的最后一个元素。在展开状......
  • 29-Vue脚手架-mixin 混入
    mixin混入功能:可以把多个组件共用的配置提取成一个混入对象使用混合:1)第一步,定义混入新建一个JS文件,比如mixin.jssrc/mixin.js//分别暴露exportconsthunhe1={methods:{showName(){alert(this.name)}},mounted(){......
  • vue3 watch 用法
    <scriptsetup>import{ref,computed,watch}from'vue'constnum=ref(1)constname=ref('ming')constobj=ref({name:'小明',age:30})//watch简单类型//watch(num,(newValue,oldVal......
  • [Vue]computed和watch的区别
    computed和watch之间的区别: 1.computed能完成的功能,watch都可以完成。 2.watch能完成的功能,computed不一定能完成,例如:watch可以进行异步操作。两个重要的小原则: 1.所有被Vue管理的函数,最好写成普通函数,这样this的指向才是vm或组件实例对象。 2.所有不......
  • linux中执行uefi runtime service call的内存上下文切换
    当linuxkernel从UEFI启动之后尽管bootservice退出了但是仍然可以使用runtimeservice。这就引发了一个问题:存在于uefi内存空间的code如何被kernel调用。首先找一个调用efiruntimeservice的例子:staticvoidefi_call_rts(structwork_struct*work){...switch(e......
  • vue中如何使用svg,以及碰到的相应问题
    安装cnpminstallsvg-sprite-loader--save-dev创建svg文件夹存放svg图标创建icons文件夹,在icons文件夹下创建svg文件夹存放本地svg图标。vue.config.js中配置svg图片config.module.rule("svg").exclude.add(resolve("src/icons")).end();config.module.rule......
  • vue3 elementplus table表格内添加checkbox和行号
    1.仅添加复选框<el-table-columntype="selection"width="55"></el-table-column>2.添加复选框和文字行号在一列<el-table-column><template#header><el-checkboxv-model="selectAll"@change="han......
  • vue 首次加载项目,控制台报错: Redirected when going from "/" to "/login"
    第一次加载加载页面时报错如下:Redirectedwhengoingfrom"/"to"/login"viaanavigationguard. ![image](https://img2023.cnblogs.com/blog/1880163/202310/1880163-20231025113840444-1010075971.png)后续在地址栏直接添加/login,index,错误页面等均正常无报错.路由......
  • Jenkins配置java和vue构建环境
    jdk,maven,node,localtime等配置可通过挂载的方式进行配置前提条件是虚拟机中已配置好jdk,maven,node等环境注意自己虚拟机相关环境配置的路径以下样例为我自己的虚拟机中的配置路径-v宿主机(虚拟机)路径:容器路径dockerrun--namejenkins-p28081:8080-p50000:50000-v/v......
  • vue3 动态加载组件
    <el-dropdownstyle="margin:0px"><el-buttontype="primary">视图</el-button><template#dropdown><el-dropdown-menu><el-dropdown-itemv-for="dropItemindropI......