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

依然范特西╮ 提交于 2019-12-10 13:53:39

问题


Let's say i have the following appRoutingModule:

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

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

When compiling this with the ngc cli command it will give the following error:

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

I tried putting it inside an exported const:

export const routerModule =  RouterModule.forRoot( routes );

But that will give this error:

Error: Error encountered resolving symbol values statically

What is the workaround/fix to get this working? How do i define my routes if i can't pass it to the RouterModule?

I'm using versions:

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

etc.


回答1:


I resolved this by reverting angular back to version 2.3.1. Looks like it's broken in the 2.4.0 release..




回答2:


Why your code doesn't import neither BrowserModule nor CommonModule? Have you tried this?

@NgModule( { imports: [BrowserModule, ApplicationsModule, RouterModule.forRoot( [ { path: '', component: ApplicationlistComponent }]) ], declarations: [ApplicationlistComponent], bootstrap: [ApplicationlistComponent] } ) export class AppRoutingModule { } The other thing to try is having the router's code in a separate file, e.g. app.routing.ts:

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

export const routing = RouterModule.forRoot(routes);

and in @NgModule:

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


来源:https://stackoverflow.com/questions/41463860/how-to-use-routermodule-forroot-in-angular2-compiler-cli-ngc-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!