Update component after router.navigate

I am trying to move from one component to another component using:

this.router.navigate(['/path/'], '_blank'); 

The problem is that the component I'm navigating to has already been created and its ngOnInit method does not execute when I go back to the page. I am trying to update the data displayed on the page. This is in the ngOnInit function of the target component:

 this.sub = this._DataService.getData().subscribe(data=> { this.data= data; }); 

How do I tell the target component that the data on the page is always updated when navigating?

I use Ag-Grid to display data in a table. In the NgOnInit function:

 this.gridOptions.rowData = this.data; 

The component I'm starting from is a form for adding a new object. After sending this object, it should redirect to the grid and show the new object. That's why the Grid needs to update its data, but for some reason it only does this when I manually update it through the context menu action in the grid, and not in the navigation.

The submit function on the form is as follows:

  onSubmit(data) { this._DataService.addData(data.value).subscribe(); this.router.navigate(['/data/']); } 
+8
source share
3 answers

If you go to the same route and change only the parameter values, translate the initialization code from constructor and call it from constructor even when changing route parameters:

 constructor(private route:ActivatedRoute) { route.params.subscribe(val => this.initialize()) } 
+7
source

I fixed this by changing my onSubmit function to:

  onSubmit(data) { this._DataService.addData(data.value).subscribe(data => { this.router.navigate(['/path]); }); } 

Thanks to this question: Angular2 - redirection does not display updated data after sending

+2
source

i implemented a redirection component

this.router.navigate (['/ redirect /', window.location.pathname]);

0
source

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


All Articles