在我的Angular应用程序中,我有一个名为AppInjector的全局变量,它返回Injector。该变量ist在AppModule
中设置。Angular 2:全局AppInjector。如何避免警告“循环依赖”
export let AppInjector: Injector;
@NgModule({
declarations: [
AppComponent,
],
bootstrap: [AppComponent],
})
export class AppModule {
constructor(private injector: Injector) {
AppInjector = this.injector;
}
}
我有一些辅助函数,与AppInjector
特殊服务的帮助下得到的。辅助函数位于单独的文件中,不属于任何组件。例如:。。?
function initNodeTemplate() {
diagram.nodeTemplateMap.add(NodeCategory.Question,
GO(go.Node, "Auto",
{
click: (evt, obj) => {(AppInjector.get(MyService) as MyService).myFunction(obj)},
},
// other stuff ...
));
}
的问题是,该角的编译器警告我关于循环依赖(因为AppInjector
WARNING in Circular dependency detected: src\app\app.module.ts -> src\app\frame\frame.module.ts -> src\app\designer\designer.module.ts -> src\app\designer\designer.component.ts -> src\app\designer\helpers\templates.helper.ts -> src\app\app.module.ts
的
我怎样才能摆脱这种警告 我知道,我可以将一个服务注入一个组件,然后将该服务传递给辅助函数,在这种情况下,我可以将detailService
作为参数传递给initNodeTemplate()
,所以我不再需要AppInjector
了,但是我想避免搞乱我的功能参数与这个服务。
打破循环。很难确切地知道如何查看导致它的代码。另见https://stackoverflow.com/questions/41585863/angular2-injecting-services-in-custom-errorhandler/41585902#41585902 –