簡體   English   中英

如何在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

我嘗試將它放在導出的const中:

export const routerModule =  RouterModule.forRoot( routes );

但是這會產生這個錯誤:

Error: Error encountered resolving symbol values statically

讓這個工作的解決方法/修復方法是什么? 如果我無法將其傳遞給RouterModule,如何定義我的路由?

我正在使用版本:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM