scss提取 vue_vue 中使用sass实现主体换肤

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

{{ msg }}

Red

Green

Blue

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);

}

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

使用方式如下

.app-container {

@include themify($themes) {

color: themed('font-color');

}

}

至此,主题换肤功能完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值