Inject

Inject decorator

What it does

A parameter decorator that specifies a dependency.

How to use

@Injectable() class Car { constructor(@Inject("MyEngine") public engine:Engine) {} }

Description

For more details, see the Dependency Injection Guide.

Example

class Engine {}

@Injectable()
class Car {
  constructor(@Inject('MyEngine') public engine: Engine) {}
}

const injector =
    ReflectiveInjector.resolveAndCreate([{provide: 'MyEngine', useClass: Engine}, Car]);

expect(injector.get(Car).engine instanceof Engine).toBe(true);

When @Inject() is not present, Injector will use the type annotation of the parameter.

Example

class Engine {}

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

const injector = ReflectiveInjector.resolveAndCreate([Engine, Car]);
expect(injector.get(Car).engine instanceof Engine).toBe(true);

exported from core-index defined in core/src/di/metadata.ts

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/docs/ts/latest/api/core/index/Inject-decorator.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部