Router Navigate does not call ngOnInit when same page

前端 未结 12 1017
时光取名叫无心
时光取名叫无心 2020-11-28 03:20

I am calling router.navigate on same page with some query string parameters. In this case, ngOnInit() does not call. Is it by default or do I need

12条回答
  •  时光取名叫无心
    2020-11-28 03:27

    I've had the same issue, additionally I got the warning:

    did you forget to call `ngZone.run()`
    

    This site provided the best solution:

    import { Router } from '@angular/router';
    import { NgZone } from '@angular/core';
    
    ...
    
      constructor(
        private ngZone:NgZone,
        private _router: Router
      ){ }
    
      redirect(to) {
        // call with ngZone, so that ngOnOnit of component is called
        this.ngZone.run(()=>this._router.navigate([to]));
      }
    

提交回复
热议问题