It Took Colin Cowherd 3 Minutes to Put the City of Philadelphia in a Body Bag

So earlier today Colin Cowherd did that thing he does everyday. He went on another one of his insane, completely absurd rants. The one today was directed at Philadelphia. Not just a specific Philly…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




ProvidedIn root

Angular using providedIn and providers arrays which is based on the principle of singleton design pattern.

A singleton service in angular is a service for which there exists only one instance in an application.

There are two ways to create a single service in angular that is by using -

1. providedIn property

2. NgModule providers arrays

Use the ProvidedIn root option, when you want to register the application-level singleton service.

The root option registers the service in the Root Module Injector of the Module Injector tree. This will make it available to the entire application. This is irrespective of whether the service is lazy loaded or eagerly loaded.

Tip: just use providedIn: 'root'. Unused services won't be compiled for a module if it isn't used due to tree shaking. Declaring module-specific services seems redundant and, as we shall see, can cause problems.

· providedIn is the new Angular way of doing DI. providedIn was brought since Angular 6

· The official name is “Tree-shakeable providers” — instead of module providing all its services, it is now the service itself declaring where it should be provided

· Using providedIn: 'root' removes the need to import the library module at all, we can simply inject needed services and it just works

App-specific — use @Injectable({ providedIn: 'root' })

providedIn: ‘root’

Module-specific — register in module providers

providedIn: Module

This method is preferred because it enables Tree-shaking (Tree shaking is a step in a build process that removes unused code from a code base) of the service if nothing injects it.

Component-specific — register in component

You can see the same output by injecting the service on the module rather than every single component.

So we see that this approach is tree-shakeable. So far so good. But you will end up with circular references if you simply try to import the same module that the components consuming the clients are declared in.

Take this setup:

my-module

my-service

my-component

There is a circular dependency.

The workaround for this is to create a service module and import that into your module.

my-module

my-module-services

my-service

my-component

This is a very long-winded alternative to simply using providedIn: 'root' and letting the tree shaking do the work.

In an eagerly loaded app, the root application injector makes all of the providers in all of the modules available throughout the application.

Lazy loading is when you load modules only when you need them; for example, when routing. They aren’t loaded right away like with eagerly loaded modules. This means that any services listed in their provider arrays aren’t available because the root injector doesn’t know about these modules.

There are two injector hierarchies in Angular:

TREE-SHAKING AND @INJECTABLE()

** Pre Angular 6 **

The only way to define providers was with the providers array. Services were singletons in eagerly loaded modules but could be instatiated multiple times with lazy loaded modules.

** Angular 6+ **

Angular 6 introduced tree-shakable providers with providedIn: ‘root’.

This has 3 big advantages:

If you want to know more about this, read the excellent article by Tomas.

** Angular 9 ✨ **

Now with Angular 9 we have two new ways to define providedIn scopes:

The difference between ‘root’ and ‘platform’ is only noticeable when running multiple Angular application in the same window. Both make sure that only one singleton exists even for lazy loaded modules. But when running two applications in the same window, each application has it’s own root injector but both share the platform injector.

Add a comment

Related posts:

The Power of Python Descriptors

Python descriptor protocol is a way to specify what happens when an attribute is referenced on a model. It allows you to easily manage attribute access : we can use the Descriptors to validate the…

Why Big Companies Use APIs For Text Classification

Do you wonder why APIs for text classification are used by big companies? Here we’ll tell you the answer! Also, you will get to know an incredible API that can do that work with the less amount of…

Let your creative flag fly

Our education system leaves no room for a child to REMAIN creative. Hobbies get lost somewhere between math assignments and dissertation. There are a select few, who manage it all, but the rest of us…