填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写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();