填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

InjectionToken

建立可用於 DI 提供者的令牌。

Creates a token that can be used in a DI Provider.

檢視"說明"...

      
      class InjectionToken<T> {
  constructor(_desc: string, options?: { providedIn?: Type<any> | "root" | "platform" | "any"; factory: () => T; })
  protected _desc: string
  toString(): string
}
    

說明

每當你要注入的型別無法確定(沒有執行時表示形式)時,例如在注入介面、可呼叫型別、陣列或引數化型別時,都應使用 InjectionToken

Use an InjectionToken whenever the type you are injecting is not reified (does not have a runtime representation) such as when injecting an interface, callable type, array or parameterized type.

InjectionTokenT 上的引數化版本,TInjector 返回的物件的型別。這提供了更高級別的型別安全性。

InjectionToken is parameterized on T which is the type of object which will be returned by the Injector. This provides additional level of type safety.

      
      interface MyInterface {...}
var myInterface = injector.get(new InjectionToken<MyInterface>('SomeToken'));
// myInterface is inferred to be MyInterface.
    

當建立 InjectionToken 時,可以選擇指定一個工廠函式,該函式返回(可能透過建立)引數化型別 T 的預設值。這將使用工廠型提供者設定 InjectionToken,就像它是在應用程式的根注入器中顯式定義的一樣。如果使用需要注入依賴項的零引數工廠函式,則可以使用 inject 函式來這樣做。參見以下示例。

When creating an InjectionToken, you can optionally specify a factory function which returns (possibly by creating) a default value of the parameterized type T. This sets up the InjectionToken using this factory as a provider as if it was defined explicitly in the application's root injector. If the factory function, which takes zero arguments, needs to inject dependencies, it can do so using the inject function. See below for an example.

此外,如果指定了 factory,也可以指定 providedIn 選項,它會覆蓋上述行為,並把這些令牌標記為屬於特定 @NgModule。如上所述,'root'providedIn 的預設值。

Additionally, if a factory is specified you can also specify the providedIn option, which overrides the above behavior and marks the token as belonging to a particular @NgModule. As mentioned above, 'root' is the default value for providedIn.

Further information available in the Usage Notes...

建構函式

      
      constructor(_desc: string, options?: { providedIn?: Type<any> | "root" | "platform" | "any"; factory: () => T; })
    
引數
_desc string
options object
可選. 預設值是 `undefined`.

屬性

屬性說明
protected _desc: string宣告在建構函式中

方法

      
      toString(): string
    
引數

沒有引數。

返回值

string

使用說明

基本範例

Basic Example

普通注入令牌

Plain InjectionToken

      
      const BASE_URL = new InjectionToken<string>('BaseUrl');
const injector =
    Injector.create({providers: [{provide: BASE_URL, useValue: 'http://localhost'}]});
const url = injector.get(BASE_URL);
// here `url` is inferred to be `string` because `BASE_URL` is `InjectionToken<string>`.
expect(url).toBe('http://localhost');
    

Tree-shakable InjectionToken

      
      class MyService {
  constructor(readonly myDep: MyDep) {}
}

const MY_SERVICE_TOKEN = new InjectionToken<MyService>('Manually constructed MyService', {
  providedIn: 'root',
  factory: () => new MyService(inject(MyDep)),
});

const instance = injector.get(MY_SERVICE_TOKEN);
expect(instance instanceof MyService).toBeTruthy();
expect(instance.myDep instanceof MyDep).toBeTruthy();