Angular 12 References/ Core
🍀 Angular References


A constructor parameter decorator that marks a dependency as optional.

constructor(@Optional() service: Service) {}


The DI framework provides null if the dependency is not found. For example, the following code allows the possibility of a null result:

class Engine {}

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

const injector = ReflectiveInjector.resolveAndCreate([Car]);