{ "id": "guide/migration-injectable", "title": "Migration for missing @Injectable() decorators and incomplete provider definitions", "contents": "\n\n\n
@Injectable()
decorators and incomplete provider definitionslink@Injectable()
decorator to classes which are provided in the\napplication but are not decorated.{provide: SomeToken}
pattern\nto explicitly specify useValue: undefined
.Example for missing @Injectable()
Before migration:
\nAfter migration:
\nNote that MyThirdClass
, MyFourthClass
and MyFifthClass
do not need to be decorated\nwith @Injectable()
because they are never instantiated, but just used as a DI token.
Example for provider needing useValue: undefined
This example shows a provider following the {provide: X}
pattern.\nThe provider needs to be migrated to a more explicit definition where useValue: undefined
is specified.
Before migration:
\nAfter migration:
\n@Injectable()
necessary?linkIn our docs, we've always recommended adding @Injectable()
decorators to any class that is provided or injected in your application.\nHowever, older versions of Angular did allow injection of a class without the decorator in certain cases, such as AOT mode.\nThis means if you accidentally omitted the decorator, your application may have continued to work despite missing @Injectable()
decorators in some places.\nThis is problematic for future versions of Angular.\nEventually, we plan to strictly require the decorator because doing so enables further optimization of both the compiler and the runtime.\nThis schematic adds any @Injectable()
decorators that may be missing to future-proof your app.
useValue: undefined
necessary?linkConsider the following pattern:
\nProviders using this pattern will behave as if they provide MyService
as DI token\nwith the value of undefined
.\nThis is not the case in Ivy where such providers will be interpreted as if useClass: MyService
is specified.\nThis means that these providers will behave differently when updating to version 9 and above.\nTo ensure that the provider behaves the same as before, the DI value should be explicitly set to undefined
.
@Injectable()
decorators to classes?linkAny class that is provided must have an @Injectable()
decorator.\nThe decorator is necessary for the framework to properly create an instance of that class through DI.
However, classes which are already decorated with @Pipe
, @Component
or @Directive
do not need both decorators.\nThe existing class decorator already instructs the compiler to generate the\nneeded information.
Yes, if your library has any classes that are meant to be injected, they should be updated with the @Injectable()
decorator.\nIn a future version of Angular, a missing @Injectable()
decorator will always throw an error.
Additionally, providers in your library that follow the described {provide: X}
pattern should be updated to specify an explicit value.\nWithout explicit value, these providers can behave differently based on the Angular version in applications consuming your library.