Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 @Injectable() - how it works

I'm trying to understand the necessity of adding the @Injectable() decoration in services in angular 2.

From the documentation: https://angular.io/docs/ts/latest/guide/dependency-injection.html

Why don't we add @Injectable() to the HeroesComponent? We can add it if we really want to. It isn't necessary because the HeroesComponent is already decorated with @Component. TypeScript generates metadata for any class with a decorator and any decorator will do.

So basically you only need to add @Injectable() if no other decoration is available, because if there is a decoration of any type available, the typescript compiler will automatically generate dependency information based on the variables that you passed in constructor eg.: constructor(private logger: Logger)

Is this right? Thanks

like image 337
Doua Beri Avatar asked Jan 04 '16 23:01

Doua Beri


People also ask

How does injectable work in Angular?

Dependency injection (DI) is a paradigm. The way it works in Angular is through a hierarchy of injectors. A class receives its resources without having to create or know about them. Injectors receive instruction and instantiate a service depending on which one was requested.

What is the purpose of the injectable () decorator?

Injectable() decorator is used to inject other services or objects into your service. If your service do not have any dependencies then you don't need to add Injectable() decorator.

What does injectable do?

Marking a class with @Injectable ensures that the compiler will generate the necessary metadata to create the class's dependencies when the class is injected. The following example shows how a service class is properly marked so that a supporting service can be injected upon creation.

What is difference between @inject and @injectable?

We use the @Inject parameter decorator to instruct Angular we want to resolve a token and inject a dependency into a constructor. We use the @Injectable class decorators to automatically resolve and inject all the parameters of class constructor.


1 Answers

I think the name is a bit wierd and i really don't get why they tell you to add it for best practice. If adding it to every class is best practice, i don't get why it's needed at all. It would be easier to have the framework check every class if the constructor needs stuff injected or not. This could have been solved with a commandline property on typescript to always generate (mock) metadata.

The advice to always add it, i guess is similar to have the compiler always add metadata (which is not possible as far as i know). You could also (with vigilance) only (and always) add it to the classes that needs stuff injected - even if they have another decorator. That would probably be the most explicit solution - but hey - being sloppy can be both a bad thing and a good thing. You'll be the judge in this case.

TL;DR If you have no other decorators AND you if your constructor needs services/etc injected, then you need it.

like image 90
Per Hornshøj-Schierbeck Avatar answered Sep 27 '22 21:09

Per Hornshøj-Schierbeck