Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onsameurlnavigation 'reload' not working in angular

I am facing an issue that is not reloading the same component. After doing google I found that I need to add onsameurlnavigation 'reload' in app-routing-module.ts file, But that is also not working

below is my app-routing-module.ts code

const routes: Routes = [
  { path: 'student', component: StudentDetailsComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes,{onSameUrlNavigation: 'reload'})],
  exports: [RouterModule]
})
export class AppRoutingModule { }

Below is my router link

<a [routerLink]="['/student']">
  Student
</a>

Please help me to resolve this same page loading issue

like image 307
Rajesh Kumar Duraisamy Avatar asked May 16 '26 11:05

Rajesh Kumar Duraisamy


1 Answers

Please be noted that onSameUrlNavigation only executes guards and resolvers but does not reinitialize components.

You can use Events from the RouterModule like to handle your scenario::

constructor(private router: Router) {}

ngOnInit() {
  this.router.events.subscribe((event) => {
    if (event instanceof NavigationEnd) {
    // do some logic again when same url is clicked
    }
  });
}
like image 150
Akhil Avatar answered May 19 '26 02:05

Akhil