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

5k Views Asked by At

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.

2

There are 2 best solutions below

1
On BEST ANSWER

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

5
On

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],