Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do we need to use @Injectable on our services in Angular2?

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:

  1. @Injectable() indicates the class follows it can be injected as service.
  2. @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

like image 204
Kuan Avatar asked Dec 16 '16 23:12

Kuan


People also ask

Is it mandatory to have @injectable in service?

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.

Why do we use @injectable in Angular?

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.

Is it mandatory to use injectable on every service class?

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.

What does @injectable decorator do in Angular?

Injectablelink Decorator that marks a class as available to be provided and injected as a dependency. providedIn? Determines which injectors will provide the injectable.


1 Answers

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 throw NoAnnotationError 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 of HeroService 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.

like image 94
Paul Samsotha Avatar answered Oct 10 '22 11:10

Paul Samsotha