在路由器配置中添加一个属性skipLocationChange
,并将其设置为true
。示例代码如下:
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'dashboard', component: DashboardComponent,
children: [
{ path: '', redirectTo: 'overview', pathMatch: 'full' },
{ path: 'overview', component: OverviewComponent, outlet: 'dashboard' },
{ path: 'stats', component: StatsComponent, outlet: 'dashboard' }
]
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: []
})
export class AppRoutingModule { }
此代码中,skipLocationChange
属性被添加到路由器配置中。这将告诉路由器在更改路由时不更改URL。这是为了防止在命名的路由器出口中出现意外行为。
@NgModule({
declarations: [
AppComponent,
HomeComponent,
DashboardComponent,
OverviewComponent,
StatsComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
此代码中,AppModule
被导入了AppRoutingModule
。这是为了确保路由器可以使用上面定义的路由器配置。