填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

Optional

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.

Learn more in the "Dependency Injection Guide".

选项

使用说明

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();