When comming back to an initialized page it doesn't run ngOnInit. For example /user/profile
shows username, I go on /user/settings
change username, and came back to /user/profile
. Username on /user/profile
didn't change because ngOnInit wasn't called.
So what I want is to re-init page which has been already initialized.
This is router.service.ts
which is used for navigation:
export class RouterService {
constructor(
private router: Router,
) {
this.router.routeReuseStrategy.shouldReuseRoute = () => false;
this.router.onSameUrlNavigation = "reload";
};
async go(path: string[], options: NavigationExtras = {}) {
this.router.navigate(path, { ...options });
}
}
If I add replaceUrl: true
to the function options it works, initted page re-inits, but the url is not being saved to the browser history and browser back button doesn't work (it redirects to the beggining of the app).
This is app.module.ts
:
@NgModule({
declarations: [AppComponent],
imports: [
...
BrowserModule,
AppRoutingModule,
HttpClientModule,
],
providers: [
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
],
bootstrap: [AppComponent]
})
This is app-routing.module.ts
:
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules, onSameUrlNavigation: "reload" }),
],
exports: [RouterModule]
})
export class AppRoutingModule { }
Those are the routes:
const routes = [
{
path: "user/profile",
loadChildren: () => import("./user/profile/profile.module").then(m => m.ProfilePageModule),
canActivate: [LoggedGuard],
},
{
path: 'user/settings',
loadChildren: () => import('./user/settings/settings.module').then(m => m.SettingsPageModule),
canActivate: [LoggedGuard],
},