Angular 为什么要引入 injection token 的概念
你可以定義和使用一個 InjectionToken 對象來為非類的依賴選擇一個提供者令牌。
這里的重點是:非類。
下列例子定義了一個類型為 InjectionToken 的 APP_CONFIG .
import { InjectionToken } from '@angular/core';export const APP_CONFIG = new InjectionToken<AppConfig>('app.config');這里的 APP_CONFIG 只是一個令牌 token,或者說是一個 place holder.
可選的參數(shù) 和令牌描述 app.config 指明了此令牌的用途。
接著,用 APP_CONFIG 這個 InjectionToken 對象在組件中注冊依賴提供者。
語義是,消費者代碼里,注入 APP_CONFIG 的令牌,則運行時,令牌會被實際的值 HERO_DI_CONFIG 取代。這個 HERO_DI_CONFIG 不是一個 Angular class, 所以只能以 injection token 的方式注冊提供者。
現(xiàn)在,借助參數(shù)裝飾器 @Inject(),你可以把這個配置對象注入到構造函數(shù)中。
constructor(@Inject(APP_CONFIG) config: AppConfig) {this.title = config.title; }接口和依賴注入
雖然 TypeScript 的 AppConfig 接口可以在類中提供類型支持,但它在依賴注入時卻沒有任何作用。在 TypeScript 中,接口是一項設計期工件,它沒有可供 DI 框架使用的運行時表示形式或令牌。
當轉(zhuǎn)譯器把 TypeScript 轉(zhuǎn)換成 JavaScript 時,接口就會消失,因為 JavaScript 沒有接口。
由于 Angular 在運行期沒有接口,所以該接口不能作為令牌,也不能注入它。
因此,下列的代碼是不合法的:
// Can't use interface as provider token [{ provide: AppConfig, useValue: HERO_DI_CONFIG })]我們不能把 interface 本身作為一個令牌,因此 Angular 引入了 injection token 的概念。
同樣,下列的代碼亦不合法,因為 interface 不能作為構造函數(shù)的輸入?yún)?shù)類型注入。因此我們需要 @Inject, 將 interface 包裹一層之后再傳入構造函數(shù)。
// Can't inject using the interface as the parameter type constructor(private config: AppConfig){ }總結
以上是生活随笔為你收集整理的Angular 为什么要引入 injection token 的概念的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qq飞车水晶许愿池多少满(PC版官方网站
- 下一篇: npm publish 发布一个 Ang