请帮忙。我试图根据设备类型延迟加载模块,但收到错误:"错误中的错误遇到静态解析符号值。只能引用初始化的变量和常量,因为模板编译器需要此变量的值...">
这是代码:
//routeting.ts
export declare var MobileDetect: any;
export var devicePath;
export var deviceType = new MobileDetect(window.navigator.userAgent);
if (deviceType.phone() != null) {
devicePath = 'app/mobile/mobile.module#MobileModule';
}
else{
devicePath = 'app/desktop/desktop.module#DesktopModule';
}
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: '', loadChildren: devicePath}
];
export const routing: ModuleWithProviders = RouterModule.forRoot(routes);
使用 AOT 路由时,应进行静态分析。您可以尝试做的是使用工厂提供 ROUTES。改变:
RouterModule.forRoot(routes)
自
RouterModule.forRoot([]);
并定义这两个提供程序:
providers: [
{provide: ANALYZE_FOR_ENTRY_COMPONENTS, multi: true, useValue: routes},
{provide: ROUTES, multi: true, useFactory: getRoutes}]
(ANALYZE_FOR_ENTRY_COMPONENTS 来自 @angular/核心,路由来自 @angular/路由器(
和以下工厂:
export function getRoutes(): Routes {
return routes;
}
此示例取自:https://github.com/angular/angular-cli/issues/5754