首頁 > web前端 > js教程 > 如何將服務注入 Angular 2 中的其他服務?

如何將服務注入 Angular 2 中的其他服務?

Mary-Kate Olsen
發布: 2024-11-15 10:44:02
原創
1043 人瀏覽過

How can I inject services into other services in Angular 2?

Injecting Services into Other Services in Angular 2 (Beta)

Injecting services into components is straightforward using the @Component decorator. However, injecting services outside of components poses a challenge.

Problem Statement

In Angular 2, we want to avoid manually instantiating services within other services, as seen in the following code snippet:

export class MyFirstSvc {

}

export class MySecondSvc {
    constructor() {
        this.helpfulService = new MyFirstSvc();
    }
}

export class MyThirdSvc {
    constructor() {
        this.helpfulService = new MyFirstSvc();
    }
}
登入後複製

Solution

The solution involves using the @Injectable decorator on the services that we wish to inject. This decorator prepares the constructor parameters of the service for dependency injection.

Injector Hierarchy

To understand how injection works, it's essential to grasp the concept of the injector hierarchy:

  • Application Injector: Configured using the second parameter of the bootstrap function.
  • Component Injector: Configured using the providers attribute of the component. It can reference providers defined in the parent injector.

When injecting a service into a component or another service, Angular2 searches for the provider in the following order:

  1. Component Injector
  2. AppComponent Injector
  3. Application Injector

Provider Sharing

The injector hierarchy allows for controlled sharing of service instances:

  • Application-level provider: Shared across the entire application.
  • Component-level provider: Shared within the component, its child components, and the services involved in its dependency chain.

Example

@Injectable()
export class Service1 {
  constructor(service2:Service2) {
    this.service2 = service2;
  }

  getData() {
    return this.service2.getData();
  }
}
登入後複製
@Injectable()
export class Service2 {

  getData() {
    return [{ message: 'message1' }, { message: 'message2' }];
  }
}
登入後複製

In this example:

  • Service1 depends on Service2.
  • When instantiating Service1 in the ChildComponent, Angular2 first searches for Service1 in the ChildComponent injector, then in the AppComponent injector, and finally in the Application injector.
  • Similarly, when instantiating Service2 within Service1, Angular2 follows the same hierarchy.
  • The injector hierarchy and provider positioning allow for flexible dependency sharing based on application needs.

Resources

  • [Angular2 Hierarchical Dependency Injection Guide](https://angular.io/docs/ts/latest/guide/hierarchical-dependency-injection.html)
  • [Plunkr Example](https://plnkr.co/edit/PsySVcX6OKtD3A9TuAEw?p=preview)

以上是如何將服務注入 Angular 2 中的其他服務?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板