Reset or reload the same page in Angular 2

I'm trying to reload the same url in Angular 2 using router.navigate

but it doesn't work.

URL: http: // localhost: 3000 / page1

Scenario: I am on http: // localhost: 3000 / landing and on click of a button will pass the routing parameter which should reload the page.

Example:

Suppose the user is on page 1 (Modify Form) and the url is read as localhost:3000/page1

and a Create New button appears by clicking a button passing a routing parameter using

let navigationExtras: NavigationExtras = { queryParams: { "refresh": "Y" } }; 
this.router.navigate(['page1'], navigationExtras);

      

+3


source to share


2 answers


You just need to call window.location.reload();



0


source


ngOnInit is called only when the component is created. When navigating to the same page (with different URL parameters), the ngOnInit method will not be called. You must use an activated Param Observable



import { ActivatedRoute } from '@angular/router';

export class MyComponent implements OnInit {
  constructor(private _activatedRoute: ActivatedRoute){}

  ngOnInit(){
    this._activatedRoute.params.subscribe(
      params => {
        // this is called everytime the url changes
      }
   }
}

      

0


source







All Articles