如何在 Angular2 compiler/cli ngc 命令中使用 RouterModule.forRoot()

How to use RouterModule.forRoot() in Angular2 compiler/cli ngc command

假设我有以下 appRoutingModule:

export const routes: Route[] = [
  {
    path: '',
    component: ApplicationlistComponent
  }
];

@NgModule( {
  imports: [ ApplicationsModule, RouterModule.forRoot( routes ) ],
  exports: [ RouterModule ],
  declarations: [ApplicationlistComponent]
} )
export class AppRoutingModule {
}

使用 ngc cli 命令编译时会出现以下错误:

Error: Error encountered resolving symbol values statically. Calling function 'RouterModule', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol AppRoutingModule in C:/git/mxt-frontend/landing-page-client/src/client/app/app-routing.module.ts, resolving symbol AppRoutingModule in C:/git/mxt-frontend/landing-page-client/src/client/app/app-routing.module.ts

我试着把它放在导出的常量中:

export const routerModule =  RouterModule.forRoot( routes );

但是会出现这个错误:

Error: Error encountered resolving symbol values statically

什么是 workaround/fix 让这个工作?如果我不能将它传递给 RouterModule,我该如何定义我的路由?

我使用的版本:

"@angular/compiler": "~2.4.0",
"@angular/compiler-cli": "~2.4.0",
"@angular/platform-server": "~2.4.0",
"@angular/router": "~3.4.0"

等等

为什么您的代码既不导入 BrowserModule 也不导入 CommonModule? 你试过这个吗?

@NgModule( { imports: [BrowserModule, ApplicationsModule, RouterModule.forRoot( [ { path: '', component: ApplicationlistComponent }]) ], declarations: [ApplicationlistComponent], bootstrap: [ApplicationlistComponent] } ) export class AppRoutingModule { } 另一件要尝试的事情是将路由器的代码放在一个单独的文件中,例如app.routing.ts:

const routes: Routes = path: '',
    component: ApplicationlistComponent

export const routing = RouterModule.forRoot(routes);

在@NgModule 中:

@NgModule( {
  imports: [BrowserModule, ApplicationsModule, 
           routing
],
  declarations: [ApplicationlistComponent],

我通过将 angular 恢复到版本 2.3.1 解决了这个问题。 看起来它在 2.4.0 版本中被破坏了..