2013-04-15 71 views
19

某些機構知道如何在SASS中使用嵌套混合函數或函數? 我有這樣的事情:SASS中的嵌套混合函數或函數

@mixin A(){ 
    do something.... 
} 

@mixin B($argu){ 
    @include A(); 
} 
+2

是的,你做得對。你可以在第二個調用第一個mixin。 [檢查這支筆](http://codepen.io/crazyrohila/pen/vuljA)。 – crazyrohila

+0

哦,謝謝!我試過這個,但沒有工作,也許我的紅寶石正在崩潰。我會嘗試重新安裝。謝謝。 (codepen是驚人的,尚未知道) – iLevi

+0

是的。 :) – crazyrohila

回答

3

可以多巢混入,你也可以使用佔位內混入..

@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; 
} 
+1

必須注意的是,mixin的順序非常重要,即如果您在上述代碼的底部移動'@mixin a {...}',sass會生成編譯錯誤'Undefined mixin'a'' – dkjain

1

正如在其他的答案中提到,你可以在其他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; 
} 

作用域混入意味着你可以有一個名爲在不同的範圍相同,而不引起的衝突多次混入。