All:
Im pretty new to Angular2, when I reach dependency injection section, that @Injectable()
notation make me a lil confused. Just want to make sure my understanding about @Injectable() is correct:
@Injectable()
indicates the class follows it can be injected as service.@Injectable()
indicates the class follows it has other @Injectable()
defined service injected into.Is this pretty much what @Injectable()
means? Anything else needs to pay special attention about this notation?
And We can not decide which case happens(or both happen) until we look into the class definition, is it correct?
Thanks
The @Injectable decorator is not compulsory to add if you don't use the 'providedIn' option. The @Injectable decorator together with the 'providedIn' option means the service should not be added within the providers' array of a module.
The @Injectable() decorator defines a class as a service in Angular and allows Angular to inject it into a component as a dependency. Likewise, the @Injectable() decorator indicates that a component, class, pipe, or NgModule has a dependency on a service. The injector is the main mechanism.
No. The @Injectable() decorator is not strictly required if the class has other Angular decorators on it or does not have any dependencies. But the important thing here is any class that is going to be injected with Angular is decorated.
Injectablelink Decorator that marks a class as available to be provided and injected as a dependency. providedIn? Determines which injectors will provide the injectable.
I don't know who wrote the documentation for @Injectable or when it was written, but it is completely misleading:
A marker metadata that marks a class as available to
Injector
for creation. ...Injector
will throwNoAnnotationError
when trying to instantiate a class that does not have@Injectable
marker.
You can see that this is not true in this Plunker. So for your question, (1) is false.
What the @Injectable
annotation actually does is provide metadata to Angular about what it needs to inject to the service. If the service doesn't need anything injected, then the service doesn't need the metadata. But if it does require injection, and doesn't have the metadata, you will get an error, as Angular can't resolve the parameters to inject.
In the documentation for dependency injection, they got it half right (with contradicting statements).
Why @Injectable()?
@Injectable()
marks a class as available to an injector for instantiation. Generally speaking, an injector will report an error when trying to instantiate a class that is not marked as@Injectable()
.As it happens, we could have omitted
@Injectable()
from our first version ofHeroService
because it had no injected parameters. But we must have it now that our service has an injected dependency. We need it because Angular requires constructor parameter metadata in order to inject a Logger.
If that doesn't sound contradictory to you, I don't know what does. The first paragraph sounds like the previous @Injectable
documentation, while the second paragraph got it right.
Just keep in mind that it is just recommended that we always add the @Injectable
decorator on all our services as we may later decide that we need to add dependency parameters, but forget the add the @Injectable
when we do so.
We recommend adding
@Injectable()
to every service class, even those that don't have dependencies and, therefore, do not technically require it. Here's why:
- Future proofing: No need to remember
@Injectable()
when we add a dependency later.- Consistency: All services follow the same rules, and we don't have to wonder why a decorator is missing.
So your number (2) is correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With