angular4路由没有保持刷新

时间:2017-03-30 17:37:05

标签: angular angular-cli typescript2.0 angular4-router

重新加载页面(F5或在地址栏中键入URL)时,不保留当前路径。我有两个独立的模块,主应用程序模块,然后是仪表板模块。这些模块每个都有自己导入的路由模块。如果我导航到http://localhost,它会重定向到http://localhost/dashboard/overview,但如果我通过地址栏导航到任何其他路线,则会重定向回http://localhost/dashboard/overview

单击使用routerlinks的页面上的任何链接都可以正常工作。

我正在使用最新的角度4和最新的角度cli。它在更新之前工作,从角度2到角度4。

以前版本的库:

"@angular/core": "^2.3.1",
"@angular/router": "^3.3.1",

当前版本的库:

"@angular/core": "^4.0.0",
"@angular/router": "^4.0.0",

APP-routing.module.ts

const routes: Routes = [
  {
    path: '',
    redirectTo: '/dashboard/overview',
    pathMatch: 'full'
  },
  {path: 'logs', component: LogsComponent},
  {path: 'search', component: SearchComponent},
  {path: 'metrics', component: MetricsComponent}
];

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

仪表板routing.module.ts

const dashboardRoutes: Routes = [
  {
    path: 'dashboard',
    component: DashboardComponent,
    children: [
      {path: 'overview', component: OverviewComponent},
      {path: 'partition/:type', component: DashboardPartitionComponent},
      {path: 'latency', component: DashboardLatencyComponent},
    ]
  }
];

@NgModule({
  imports: [ RouterModule.forChild(dashboardRoutes)],
  exports: [ RouterModule ]
})
export class DashboardRoutingModule {}

编辑:

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

1 个答案:

答案 0 :(得分:1)

原来我有一个劫持路由器并重定向到注入参数的调用。在更新之前,this.router.url将返回正确的url。现在由于某种原因它总是返回root。