Injectable
标记性元数据,表示一个类可以由 Injector
进行创建。
Decorator that marks a class as available to be provided and injected as a dependency.
选项 | 说明 |
---|---|
providedIn? | 通过与 Determines which injectors will provide the injectable, by either associating it with an |
参见
使用说明
使用 @Injectable
标记一个类可确保编译器将在注入类时生成必要的元数据,以创建类的依赖项。
Marking a class with @Injectable
ensures that the compiler will generate the necessary metadata to create the class's dependencies when the class is injected.
下面的例子展示了如何正确的把服务类标记为可注入的(Injectable)。
The following example shows how a service class is properly marked so that a supporting service can be injected upon creation.
@Injectable()
class UsefulService {
}
@Injectable()
class NeedsService {
constructor(public service: UsefulService) {}
}
const injector = Injector.create({
providers:
[{provide: NeedsService, deps: [UsefulService]}, {provide: UsefulService, deps: []}]
});
expect(injector.get(NeedsService).service instanceof UsefulService).toBe(true);