Вы можете использовать onSameUrlNavigation
от Angular router:
@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();
}
}
}
И вот, теперь у вас есть возможность перезагрузки.Надеюсь это поможет.К сожалению, документы не очень ясны по этому поводу.