How to reload/refresh the data when navigate to same route in Angular 9

331 阅读1分钟

💬 Tech Talks › How to reload/refresh the data when navigate to same route in Angular 9

3Vote UpVote Down

Varun Sharma asked 1 year ago

I have an issue with the route
Routing File


const routes = [
  {
    path: '',  // base route is `setting`
    component: SettingViewComponent, 
    children: [
      {
        path: 'services',
        component: ServiceListComponent
      },
      {
        path: 'general',
        component: GeneralSettingsComponent
      },
    ]
 }
];

When I am on /setting/services and hits the /setting route the SettingViewComponent code doesn’t get initiate again.
I have a redirection logic in SettingViewComponent which needs to be re-initiate.

Share this:

Question Tags: angular 9

1 Answers

11Vote UpVote Down

Best Answer

Jake answered 1 year ago

You can use the Router and override the shouldReuseRoute from the routeReuseStrategy
method which returns false statement.


import { Router } from '@angular/router';
...
export class SettingViewComponent implements OnInit {

 constructor(
   private router: Router
 ) { }

 public ngOnInit(): void {
    this.router.routeReuseStrategy.shouldReuseRoute = () => false;
}

this might solve your problem.