[2019-11-20] service and DI Profile

DI - dependency injection

 

When Angular create a new instance of a component class, it will be by looking at the component class constructor to decide which services or other dependencies of the components depend on, such as the above heroService

When Angular found that a component dependent on a service, it will first insist on whether the injector has been that any existing instances of the service. If the requested service does not yet exist, the injector will use before making a registered service providers, and add it to the injector, and then put the service back to the Angular

When all the requested service and returns resolved, the Angular Examples of these services can use parameters, call the constructor of the assembly

Provide services

For any service to use, you must register at least one vendor.

① services in their own metadata to their own registered as a provider, would allow themselves available anywhere

 

In providing a service roots, Angular creates a single shared instance, and inject it into any desired its class. This data provider registered in @Injectable cell method also allows the angular size can be optimized by removing those who never used services

② provider can be registered in a particular module, the same instance of the service will be available on all the components of NgModule

 

③ also be a new instance of the component level registered provider, you will provide this service for each new instance of the component

 

 

Guess you like

Origin www.cnblogs.com/QQ-lala/p/11896783.html