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



假设我有以下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

有什么解决方法/修复程序可以使其正常工作?如果我无法将路由传递给路由器模块,如何定义我的路由?

我正在使用的版本:

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

等。

我通过将角度恢复到2.3.1版本来解决此问题。看起来它在 2.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],

相关内容

  • 没有找到相关文章

最新更新