【问题标题】:angular4 Routes not being kept on refreshangular4路由没有保持刷新
【发布时间】:2017-08-24 18:52:51
【问题描述】:

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

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

我正在使用最新的 angular 4 和最新的 angular-cli。它在从 angular 2 更新到 angular 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 {}

dashboard-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"
    ]
  }
}

【问题讨论】:

  • 您使用的是什么 HTTP 服务器,您是否已将其配置为支持 HTML5 Pushstate?
  • 我正在使用带有 webpack 的 Node.js。一切都是默认的,由 angular-cli 创建
  • 另请注意,删除默认路由时仍然会发生这种情况

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-02-24
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 2019-05-28
    • 2018-02-28
    • 2017-02-01
    • 2016-01-30
    • 2020-02-26
    相关资源
    最近更新 更多