CustomFunction<sync>
類型參數
-
sync extends "sync" | "async"
類型宣告
-
- (args: Value[]): PromiseOr<Value, sync>
-
實作自定義 Sass 函數的回呼。這可以傳遞給 functions。
const result = sass.compile('style.scss', {
functions: {
"sum($arg1, $arg2)": (args) => {
const arg1 = args[0].assertNumber('arg1');
const value1 = arg1.value;
const value2 = args[1].assertNumber('arg2')
.convertValueToMatch(arg1, 'arg2', 'arg1');
return new sass.SassNumber(value1 + value2).coerceToMatch(arg1);
}
}
});拋出
任何 - 此函數可能會拋出錯誤,Sass 編譯器會將其視為函數呼叫失敗。如果例外物件具有
message
屬性,它將被用作包裝例外訊息;否則,將使用例外物件的toString()
。這表示自定義函數可以安全地拋出純字串。參數
-
args: Value[]
由函數呼叫者傳遞的參數陣列。如果函數接受任意參數,則最後一個元素將是 SassArgumentList。
返回 PromiseOr<Value, sync>
函數的結果。這可以是
Promise
的形式,但如果是,則該函數只能傳遞給 compileAsync 和 compileStringAsync,而不是 compile 或 compileString。 -
CustomFunction<'sync'>
必須同步返回,但作為回報,除了 compileAsync 和 compileStringAsync 之外,它還可以傳遞給 compile 和 compileString。CustomFunction<'async'>
可以同步或非同步返回,但它只能與 compileAsync 和 compileStringAsync 使用。