【发布时间】: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