当前位置: 移动技术网 > IT编程>开发语言>JavaScript > vue 中使用sass实现主体换肤

vue 中使用sass实现主体换肤

2019年04月05日  | 移动技术网IT编程  | 我要评论
有如下代码要实现换肤功能 这里通过一个下拉框应用不用主题 首先我们把主题变量抽取出来 这里包含三个主题red,gredd,blue,每个主题内的font-color变量对应不同的值, 然后我们写一个主题化的mixin,包括一个themed函数 这段代码的功能主要是对需要主体化的地方,对样式根据不同主 ...

有如下代码要实现换肤功能

<template>
  <div class="app-root" :class="themeclass">
    <div class="app-container">
      <p>{{ msg }}</p> 
      <select v-model="theme">
        <option value="red">red</option>
        <option value="green">green</option>
        <option value="blue">blue</option>
      </select>
    </div>
  </div>
</template>
<script>
export default {
  name: 'app',
  data() {
    return {
      msg: 'dynamic themes',
      theme: 'red'
    }
  },
  computed: {
    themeclass() {
      return `theme-${this.theme}`;
    }
  }
}

这里通过一个下拉框应用不用主题

首先我们把主题变量抽取出来

$themes: (
  red: (
    font-color: red,
  ),
  green: (
    font-color: green 
  ),
  blue: (
    font-color: blue
  ),
);

这里包含三个主题red,gredd,blue,每个主题内的font-color变量对应不同的值,

然后我们写一个主题化的mixin,包括一个themed函数

@mixin themify($themes: $themes) {
  @each $theme-name, $map in $themes {

    .theme-#{$theme-name} & {
      $theme-map: () !global;
      @each $key, $value in $map {
        $theme-map: map-merge($theme-map, ($key: $value)) !global;
      }

      @content;

      $theme-map: null !global;
    }
  }
}

@function themed($key) {
  @return map-get($theme-map, $key);
}

  这段代码的功能主要是对需要主体化的地方,对样式根据不同主题的变量进行替换,然后复制一份样式代码

使用方式如下

<style lang="scss" scoped>
@import './styles/theming';
.app-container {
  @include themify($themes) {
    color: themed('font-color');
  }
}
</style>

至此,主题换肤功能完成

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网