简体   繁体   中英

OnInit method not getting called when I navigate to url in ngZone

I have two components and I am trying to navigate to URL in NgZone method. But issue that I am facing is OnInit method is not getting called when I navigate. Below id the code:

ngOnInit() {
    this._zone.run(() => {
        window['navigate'] = () => {
          this.route.navigate(['/users'];
        };
    });
  }

Using above code I am able to navigate to /users URL. But UsersComponent onInit() method is not getting called on navigation.

I have looked for the solution and it seems like in ngZone I am navigating out of angular scope. So I tried using ChangeDetectorRef but its not working as I am navigating away from URL. Please let me know if my understanding is correct and suggest me possible solution.

Try putting the router.navigate() into a separate "Zone":

import:

import { NgZone } from "@angular/core";

inject:

constructor(private zone: NgZone) { ... }

apply:

this.zone.run(() => {
    this.router.navigate(["/users"]);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM