您可以使用来自 Angular 路由器的onSameUrlNavigation:
@ngModule({
imports: [RouterModule.forRoot(routes, {onSameUrlNavigation: ‘reload’})],
exports: [RouterModule],
})
然后在您的路线上使用runGuardsAndResolvers 并将其设置为始终:
export const routes: Routes = [
{
path: 'my-path',
component: MyComponent,
children: [
{
path: '',
loadChildren: './pages/my-path/mycomponent.module#MyComponentModule',
},
],
canActivate: [AuthenticationGuard],
runGuardsAndResolvers: 'always',
}
]
通过这两项更改,您的路由器已配置完毕。现在你需要在你的组件中钩入NavigationEnd:
export class MyComponent implements OnInit, OnDestroy{
// ... your class variables here
navigationSubscription;
constructor(
// … your declarations here
private router: Router,
) {
// subscribe to the router events - storing the subscription so
// we can unsubscribe later.
this.navigationSubscription = this.router.events.subscribe((e: any) => {
// If it is a NavigationEnd event re-initalise the component
if (e instanceof NavigationEnd) {
this.initialiseMyComponent();
}
});
}
initialiseMyComponent() {
// Set default values and re-fetch any data you need.
}
ngOnDestroy() {
// avoid memory leaks here by cleaning up after ourselves. If we
// don't then we will continue to run our initialiseInvites()
// method on every navigationEnd event.
if (this.navigationSubscription) {
this.navigationSubscription.unsubscribe();
}
}
}
你去吧,你现在有重新加载的能力。希望这可以帮助。不幸的是,文档对这些不是很清楚。