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

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 with the ngc cli command, it will throw 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 in an exported constant:

 export const routerModule = RouterModule.forRoot( routes ); 

But this will give this error:

 Error: Error encountered resolving symbol values statically 

What workaround / fix to make this work? How to determine my routes if I cannot pass it to the RouterModule?

I am using versions:

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

and etc.

+5
source share
2 answers

I solved this by returning angular back to version 2.3.1. It looks like it is broken in version 2.4.0 ..

+2
source

Why doesn't your code import either BrowserModule or CommonModule? Have you tried this?

@NgModule( { imports: [BrowserModule, ApplicationsModule, RouterModule.forRoot( [ { path: '', component: ApplicationlistComponent }]) ], declarations: [ApplicationlistComponent], bootstrap: [ApplicationlistComponent] } ) export class AppRoutingModule { } Another thing to try to have the router code in a separate file, for example. 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], 
+1
source

Source: https://habr.com/ru/post/1262290/


All Articles