如何將服務注入 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:
- Component Injector
- AppComponent Injector
- 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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

實現視差滾動和元素動畫效果的探討本文將探討如何實現類似資生堂官網(https://www.shiseido.co.jp/sb/wonderland/)中�...

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

探索前端中類似VSCode的面板拖拽調整功能的實現在前端開發中,如何實現類似於VSCode...

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。
