mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
committed by
Pawel Kozlowski
parent
c365cfab03
commit
bb40a08164
@@ -57,7 +57,7 @@ class HeroService {}
|
||||
|
||||
When you provide the service at the root level, Angular creates a single, shared instance of the `HeroService` and injects it into any class that asks for it. Registering the provider in the `@Injectable` metadata also allows Angular to optimize an app by removing the service from the compiled application if it isn't used, a process known as tree-shaking.
|
||||
|
||||
## Providing dependency
|
||||
## Injecting a dependency
|
||||
|
||||
The most common way to inject a dependency is to declare it in a class constructor. When Angular creates a new instance of a component, directive, or pipe class, it determines which services or other dependencies that class needs by looking at the constructor parameter types. For example, if the `HeroListComponent` needs the `HeroService`, the constructor can look like this:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user