SASS @forward的使用
作用
通过 @forward
加载一个模块的成员,并将这些成员当作自己的成员对外暴露出去,类似于类似于 es6 的 export …,通常用于跨多个文件组织 Sass 库
转发、合并scss
转发
创建forward/_common.scss
$font-size:14px !default;
* {
margin: 0;
padding: 0;
font-size: $font-size;
color: #333;
}
@function column-width($col, $total) {
@return percentage($col/$total);
}
@mixin bgColor($bg-color:#f2f2f2) {
background-color: $bg-color;
}
创建启动合并bootstrap.scss
@forward 'uses/common';
使用
@use 'bootstrap';
.body {
font-size: bootstrap.$font-size;
width: bootstrap.column-width(3, 12);
@include bootstrap.bgColor('#F00');
}
合并
新增一个_global.scss
$font-size:28px;
@mixin base($color:#F00) {
color: $color;
}
.gclass {
background-color: #F00;
}
统一转发
@forward 'uses/common';
@forward 'uses/global';
使用
@use 'bootstrap';
.body {
font-size: bootstrap.$font-size;
width: bootstrap.column-width(3, 12);
@include bootstrap.bgColor('#F00');
@include bootstrap.base('#000');
}
**问题:**当多个被转发的文件存在相同变量、函数、混入时会有问题
选择性转发
默认情况下,@forward
会将一个模块中所有成员都转发,如果只想转发某些成员,当你不想转发所有变量、函数、混入时,可使用
@forward "