填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

Inject

类构造函数中依赖项参数上的参数装饰器,用于指定依赖项的自定义提供者。

Parameter decorator on a dependency parameter of a class constructor that specifies a custom provider of the dependency.

选项说明
token

一个 DI 令牌,映射到要注入的依赖项。

A DI token that maps to the dependency to be injected.

参见

选项

一个 DI 令牌,映射到要注入的依赖项。

A DI token that maps to the dependency to be injected.

      
      token: any
    

使用说明

下面的示例显示了一个类构造函数,该构造函数使用参数装饰器指定了依赖项的自定义提供者。

The following example shows a class constructor that specifies a custom provider of a dependency using the parameter decorator.

如果有 @Inject(),则注入器将参数的类型注解用作提供者。

When @Inject() is not present, the injector uses the type annotation of the parameter as the provider.

      
      class Engine {}

@Injectable()
class Car {
  constructor(public engine: Engine) {
  }  // same as constructor(@Inject(Engine) engine:Engine)
}

const injector = Injector.create(
    {providers: [{provide: Engine, deps: []}, {provide: Car, deps: [Engine]}]});
expect(injector.get(Car).engine instanceof Engine).toBe(true);