mercredi 9 mai 2018

angular 6 - creating a non root module scoped service

As far as i know until angular 6 , all the @Ngmodule providers where registered on the root injector and were served in the main bundle even if only lazy loaded modules used them.

The only only exception to this was if we wanted to create a non singleton services in a component level: https://angular.io/guide/dependency-injection#injectable-ngmodule-or-component

I want to create a singleton service which will be visible only to a specific module (not to the root module), and as a result of that will not be served in the main eagerly loaded bundle.

In saw that in angular 6 the module will no longer need to refer the service via the "providers" , but rather the service will now refer to the module.

This can be done by the "@Injectable" annotation and the "provideIn" attribute.

I didn't find a good and clear example of how can i add a module name which is not 'root', something like this:

@Injectable({ provideIn: <MyLocalModule>})
export class SimpleServiceForLocalUseOnly { […] }

List of searched references:

https://blog.angular.io/version-6-of-angular-now-available-cc56b0efa7a4

https://jaxenter.com/new-angular6-143995.html

https://www.ngdevelop.tech/angular-6-features/

https://blog.ninja-squad.com/2018/05/04/what-is-new-angular-6/

http://ankitsharmablogs.com/getting-started-with-angular-6-0/

https://www.youtube.com/watch?v=Xr5l7lT--YU

Aucun commentaire:

Enregistrer un commentaire