Vue+scss设置主题切换

表现形式

.demo[data-theme="default"]{
	color:#000;
}
//不同主题
.demo[data-theme="primary"]{
	color:#fff;
}

SCSS

定义主题
$themes:(
	default:(
    	color:#000,
    ),
    primary:(
    	color:#fff,
    ),
);
混入器
//定义混入器
@mixin theme{
    @each $name,$map in $themes{
		$map:$map !global;	//提升为全局变量
        &[data-theme="#{$name}"]{
			@content;		//接收混入
        }
    }
}
//设置主题数据
@function t($key){
	@return map-get($map,$key);
}
封装模块
@mixin box{
    @include theme{
		color:t('color');
        font-size:t("size");
    }
}

@include box;//使用

Vue

动态主题
<template>
	<div class="demo" :data-theme="theme"></div>
</template>
<script>
	export deafult{
		props:{
            /** 组件接收主题类型参数 */
            theme:{
				type:String,
            	default:'default',
            }
        }
    }
</script>
<style lang="scss" scope>
    @import 'theme.scss';	//引入scss文件
    .demo{
		/** 调用混入器 */
        @include theme{
			color:t('color');
        }
    }
</style>

下载支持

  • 安装scss

    npm i node-sass@4.14.1 -S
    npm i sass-loader@7.3.1 -S
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值