在我的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 –