1
我試圖提供一種方法來定製服務,通過使用靜態forRoot(),但...不支持角度4.x函數調用。考慮與參考替換函數或lambda來導出函數
我碰到下面的錯誤 - 即使我已導出menuServiceFactory函數以用作工廠。
我也試着將它作爲const值導出。
ERROR in Error: Error encountered resolving symbol values statically. Calling function 'MenuModule', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol AppModule in ...
相關的代碼 -
import { NgModule, ModuleWithProviders, SkipSelf, Optional } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MenuComponent } from './menu.component';
import { MenuService } from './menu.service';
import { MdMenuModule, MdToolbarModule, MdButtonModule } from '@angular/material';
import { FlexLayoutModule } from '@angular/flex-layout';
import { SecurityModule, SecurityService } from '@savantly/ngx-security';
@NgModule({
imports: [
CommonModule,
MdMenuModule, MdToolbarModule, MdButtonModule, FlexLayoutModule, SecurityModule
],
exports: [
CommonModule,
MdMenuModule, MdToolbarModule, MdButtonModule, FlexLayoutModule,
SecurityModule,
MenuComponent],
declarations: [MenuComponent],
providers: []
})
export class MenuModule {
static forRoot(options?: any): ModuleWithProviders {
if (options && options.securityService) {
return {
ngModule: MenuModule,
providers: [{
provide: MenuService,
useFactory: menuServiceFactory,
deps: [options.securityService]
}]
}
} else {
return {
ngModule: MenuModule,
providers: [MenuService]
};
}
}
constructor (@Optional() @SkipSelf() parentModule: MenuModule) {
if (parentModule) {
throw new Error(
'MenuModule is already loaded. Import it in the AppModule only');
}
}
}
export function menuServiceFactory(_securityService: SecurityService): MenuService {
return new MenuService(_securityService);
}
嘗試刪除'如果else'聲明 – yurzui
感謝@yurzui - 它沒有擺脫的靜態分析錯誤。現在我需要弄清楚如何使這個條件... – Jeremy
'{提供:someToken,useValue:選項},{提供:MenuService,useFactory:menuService,代表:[someToken]}' – yurzui