0

I have a route that goes to a calculator. The person might hit calculate and the screen shows the result. Which is fine. They then may go up to the navbar and click the same calculator again, by that I mean they are clicking the same route. I would like to screen to re-route so they are in essence starting over, refreshing the screen. Of course the route does not change nor does the screen change because they are on the same route. Is there some way to force the route to re-load or refresh. I have several different routes with different components that need this. So if I need to write code to handle them all I would like it to be in a service and not in the component. Preferably I would like it when I am setting up the routes.

Maccurt
  • 12,655
  • 7
  • 32
  • 43
  • Can you try this solution - https://stackoverflow.com/a/45737269/2030471 –  Dec 17 '17 at 13:00
  • Yes that did work, though I am not sure putting that in every component you want to use it in is the best idea, I might have to put it in a service or a class I can inherit it from – Maccurt Dec 17 '17 at 21:30

2 Answers2

2

One of the cool features of Angular 5 is precisely the ability to reload/refresh the same page in case you want it. You just need to set it on your configuration

providers: [
  // ...
  RouterModule.forRoot(routes, {
onSameUrlNavigation: 'reload'
  })
]

It’s part of what’s called ExtraOptions for router. You can check the documentation here if you need more details

Hugo Noro
  • 3,145
  • 2
  • 13
  • 19
0

Implement OnInit and call ngOnInit() in the method for route.navigate()

See an example :

   export class Component implements OnInit {

          constructor() {   }

          refresh() {
            this.router.navigate(['same-route-here']);
            this.ngOnInit();   }

          ngOnInit () {

          }
   }