Angular does not call ngOnInit when programmatically routing to a page

I implemented the following console outputs to keep track of what was done and what wasn’t. Code for the page under / dataList.

export class DataList implements OnInit {

  constructor(private service: DataService) { 
    console.log("constructed");
  }

  ngOnInit() {
    console.log("inited");
    ...
  }

}

Now it works as intended, providing both outputs (i.e., designed and located) in the console when accessing the page , entering the URL , and also when accessing the page using routing from another page , as shown below.

this.router.navigateByUrl("/dataList");

( URL-, /dataElement/ 12345, 12345, ) ( ), /dataList URL-. ( F5, , .)

, , ngOnInit, , , , ( ).

, , , . , , . Googling , ( this). .

const routes: Routes = [
  { path: "", component: StartComponent },
  { path: "dataList", component: DataListComponent },
  { path: "dataElement/:id", component: DataElementComponent },
  ...
  { path: "**", component: StartComponent }
];
+4
1

, (a.k.a. dumb luck), - Angular 5 ( 4-). -, , , .

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

export class DataElement implements OnInit {

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

  shaboo() {
    ...
    this.zone.run(() => {
      this.router.navigateByUrl("/dataList");
    });
  }
}

no idea, , , . , . , , , , , - , (, , 5, ).

0

Source: https://habr.com/ru/post/1691890/


All Articles