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

Inject

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

查看"说明"...

选项说明
token

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

说明

Learn more in the "Dependency Injection Guide".

选项

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

token: any
      
      token: any
    

使用说明

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

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);
      
      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);