Angular2在运行时将AppModule中的服务注入到依赖的ngmodule的组件中
Angular2 inject a service from AppModule into dependent NgModule's component at runtime?
如果我有:
AppModule
imports: [SubModule],
providers: [AppProvidedService]
和
SubModule
declarations: [SubComponent]
和
SubComponent
constructor(appProvidedService: AppProvidedService){}
:
Uncaught Can't resolve all parameters for SubComponent: (?).
基本上,它是在说AppProvidedService不能被注入器解析。
如何构建一个NgModule,让它依赖于另一个NgModule中创建的服务,而这个服务又依赖于它?
我尝试使用OpaqueToken和接口,但我仍然有同样的问题。子模块无法看到来自父模块的内容。
最终的愿望是拥有一个可以从导入它的应用程序中接受注入的模块。这样,我就可以把特定于应用程序的行为注入到公共组件中。
我明白了。它需要OpaqueTokens。
首先,为该接口提供一个接口和OpaqueToken (app- provisied .service.interface:
)import { OpaqueToken } from "@angular/core";
export interface AppProvidedServiceInterface {
...
}
export const SERVICE_INTERFACE = new OpaqueToken("AppProvidedServiceInterface");
在AppModule的providers中:
/*
* Injection of app-specific behavior into a common module without hard dependencies:
*
* This is how you inject a service into a module that is imported
* and prevent that module from having a hard dependency on the actual
* implementation. Use an interface with an OpaqueToken. Then the
* component that needs it imports it and uses the AppInjector to get the
* instance.
*/
{
provide: SERVICE_INTERFACE,
useFactory: () => {
return new AppProvidedService();
}
}
并且在bootstrap中获取应用注入器的引用并存储它…(我们使用一个简单的单例类AppInjector(源代码在答案的末尾)):
platformBrowserDynamic().bootstrapModule(AppModule)
.then((appRef: NgModuleRef<AppComponent>) => {
AppInjector.getInstance().setInjector(appRef.injector);
},
error => console.log(error) // tslint:disable-line
);
然后,在你的SubModule中,在你想要使用被注入的东西的类中,你必须使用注入器来查找基于OpaqueToken (sub.component.ts)的类:
// DO NOT REFERENCE A CONCRETE SERVICE!
private appProvidedService: AppProvidedServiceInterface;
constructor(/* can't inject it here */) {
// Get the app-specific behavior from the service injected in the application module.
this.appProvidedService = AppInjector.getInstance().getInjector().get(SERVICE_INTERFACE);
}
现在,你的子模块有一个接口的引用,而不是一个具体的实现,AppModule可以通过providers: []
注入所需的行为!
AppInjector单:
import { Injector } from "@angular/core";
export class AppInjector {
private static instance: AppInjector;
private injector: Injector;
constructor() {
// throw new Error("Singleton - can't instantiate.");
}
public static getInstance() {
if (!AppInjector.instance) {
AppInjector.instance = new AppInjector();
}
return AppInjector.instance;
}
public setInjector(injector: Injector) {
this.injector = injector;
}
public getInjector(): Injector {
return this.injector;
}
}
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 为什么Airbnb风格指南说不鼓励依赖函数名称推断
- 将依赖外部库的UMD模块与browserfy捆绑在一起
- 如何将本地依赖项添加到npm项目中
- 如何在内联依赖项并将图像转换为dataURI的情况下完全提取网页
- jquery中的复选框依赖项
- 为什么react/redux前端项目在package.json中包含生产依赖项
- 单元测试依赖关系没有被嘲笑
- 依赖<选择>HTML和JavaScript菜单
- FRP 中 EventStreams 的循环依赖关系
- 节点模块依赖关系
- 使用多个依赖项加载只进行一次AJAX调用
- 限制npm依赖模块's require(隔离)的用法
- AngularJS依赖注入定时问题
- 在Meteor中使用具有依赖关系的NPM包
- Dojo AMD加载程序执行定义具有空/缺失依赖项的回调
- 依赖于缓慢加载javascript的UI
- AngularJS或jquery中的依赖级联下拉框列表
- 如何在jasmine测试中为有角度的项目注入依赖项
- Angular2在运行时将AppModule中的服务注入到依赖的ngmodule的组件中