填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

Optional

用於建構函式引數的引數裝飾器,將引數標記為可選依賴項。如果找不到依賴項,則 DI 框架提供 null。

Parameter decorator to be used on constructor parameters, which marks the parameter as being an optional dependency. The DI framework provides null if the dependency is not found.

檢視"說明"...

參見

說明

可以與其他修改依賴注入方式的引數裝飾器一起使用。

Can be used together with other parameter decorators that modify how dependency injection operates.

選項

使用說明

以下程式碼允許結果為空的可能性:

The following code allows the possibility of a null result:

class Engine {} @Injectable() class Car { constructor(@Optional() public engine: Engine) {} } const injector = Injector.create({providers: [{provide: Car, deps: [[new Optional(), Engine]]}]}); expect(injector.get(Car).engine).toBeNull();
      
      class Engine {}

@Injectable()
class Car {
  constructor(@Optional() public engine: Engine) {}
}

const injector =
    Injector.create({providers: [{provide: Car, deps: [[new Optional(), Engine]]}]});
expect(injector.get(Car).engine).toBeNull();