重新加载页面(F5 或在地址栏中输入 url)时,不会保留当前路由。我有两个独立的模块,主应用程序模块和仪表板模块。这些模块每个都有自己导入的路由模块。如果我导航到http://localhost它会重定向到http://localhost/dashboard/overview但如果我通过地址栏导航到任何其他路由它会重定向回http://localhost/dashboard/overview
单击使用 routerlinks 的页面上的任何链接都可以正常工作。
我正在使用最新的 angular 4 和最新的 angular-cli。它在从角度 2 更新到角度 4 之前工作。
以前版本的库:
"@angular/core": "^2.3.1",
"@angular/router": "^3.3.1",
当前版本的库:
"@angular/core": "^4.0.0",
"@angular/router": "^4.0.0",
应用程序路由.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"
]
}
}