2017-06-22 48 views
0

请帮忙。我试图延迟加载一个模块,具体取决于设备类型,但是得到一个错误:“ERROR in Error遇到静态解析符号值。只能初始化变量和常量,因为模板编译器需要此变量的值。 ”Angular 4 - 在路由中静态解析符号值时出错

下面是代码:

//routing.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); 
+0

你在干什么AOT? – JEY

+0

对不起忘了提到我在AOT建设。 –

回答

0

当使用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是从@角/芯和从@角/路由器路由)

及以下工厂:

export function getRoutes(): Routes { 
    return routes; 
} 

这个例子取自: https://github.com/angular/angular-cli/issues/5754

相关问题