有人知道我如何在SASS中使用嵌套的mixin或函数吗? 我有类似这样的代码:
@mixin A(){
do something....
}
@mixin B($argu){
@include A();
}
有人知道我如何在SASS中使用嵌套的mixin或函数吗? 我有类似这样的代码:
@mixin A(){
do something....
}
@mixin B($argu){
@include A();
}
您可以多层嵌套Mixin,您也可以在Mixin内部使用占位符。
@mixin a {
color: red;
}
@mixin b {
@include a();
padding: white;
font-size: 10px;
}
@mixin c{
@include b;
padding:5;
}
div {
@include c();
}
生成CSS的工具
div {
color: red;
padding: white;
font-size: 10px;
padding: 5;
}
@ mixin a {...}
移动到上述代码的底部,Sass会生成编译错误“未定义mixin'a'”。 - dkjain@include b;
中,为什么 b
不是 b()
? - ajax333221正如其他答案中提到的那样,您可以在其他mixin中包含mixin。此外,您可以限定mixin的作用域。
示例
.menu {
user-select: none;
.theme-dark & {
color: #fff;
background-color: #333;
// Scoped mixin
// Can only be seen in current block and descendants,
// i.e., referencing it from outside current block
// will result in an error.
@mixin __item() {
height: 48px;
}
&__item {
@include __item();
&_type_inverted {
@include __item();
color: #333;
background-color: #fff;
}
}
}
}
将输出:
.menu {
user-select: none;
}
.theme-dark .menu {
color: #fff;
background-color: #333;
}
.theme-dark .menu__item {
height: 48px;
}
.theme-dark .menu__item_type_inverted {
height: 48px;
color: #333;
background-color: #fff;
}
作用域混合指的是您可以在不同范围内具有相同名称的多个混合,而不会引起冲突。