2017-06-22 47 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

相關問題