What is a method to refresh a route in Angular 2 programmatically in order to trigger onInit hook?
2 Answers
Maybe there is a single method to just refresh the current route.
From:
https://angular.io/docs/ts/latest/api/router/Router-class.html
renavigate() : Promise<any>Navigates to either the last URL successfully navigated to, or the last URL requested if the router has yet to successfully navigate.
Inject the router and call the renavigate() method:
this._router.renavigate();
4 Comments
works for angular 5+ :
1) add runGuardsAndResolvers to your route:
const routes: Routes = [ { path: '', component: YourComponent, runGuardsAndResolvers: "always" } ]
2) add onSameUrlNavigation property to routes import
@NgModule({
imports: [RouterModule.forRoot(routes, {
onSameUrlNavigation: 'reload'
})],
exports: [RouterModule]
})
3) now navigating to your url using navigateByUrl('current url') reloads the page for you.
EventEmitterto allow components to subscribe changes.