Injectable

A marker metadata that marks a class as available to Injector for creation.

标记性元数据,表示一个类可以由 Injector 进行创建。

选项说明
providedIn

说明

For more details, see the "Dependency Injection Guide".

欲知详情,参见"依赖注入"

选项

providedIn: Type<any> | 'root' | null

使用说明

Example

范例

@Injectable() class UsefulService { } @Injectable() class NeedsService { constructor(public service: UsefulService) {} } const injector = ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService]); expect(injector.get(NeedsService).service instanceof UsefulService).toBe(true);

Injector will throw an error when trying to instantiate a class that does not have @Injectable marker, as shown in the example below.

Injector在试图实例化一个不带 @Injectable 标记的类时,就会抛出一个错误,如下面的例子所示。

class UsefulService {} class NeedsService { constructor(public service: UsefulService) {} } expect(() => ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService])).toThrow();