Web Developer

Section 9 Service

Service file is wrote by TypeScript, and don’t need to add decorator @, but if the current service file is need to inject other service from other service file, it need to add decorator: @Injectable().

So right now Angular suggests add @Injectable() in any service files, no matter what it is needed to inject or not.

Service inject is a hierarchical form, such as, (1) if you claim this service in xxx.module.ts file (providers: [serviceClass]), so all this app shares the same instance; (2) if you claim this service in app.component.ts file (providers: [serviceClass]), so only it’s children components use the same instance; (3) claim this service in the other xxx.component.ts file (providers: [serviceClass]), so only it’s children components use the same instance.

Another rule is : if you instantiate several times of the service (import providers in decorator @Component({…, providers:[xxx]}), the lowest instance will overwrite the upper one.

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>