The power of Angular2 to reload a component when navigating

In Angular2 RC1, and the bottom call of the route line always causes the component to reload:

<a [routerLink]="['/page', {id: 1}]">A link </a> 

Using Angular2, not a single RC, the component does not reboot if it goes to itself with different parameters. Is there any way to return reboot behavior?

I understand another way to deal with this by subscribing to ActivatedRoute and detecting changes to variables, but this leads to component logic increasing in complexity.

+7
angular angular2-routing
source share
2 answers

Despite the fact that you mentioned "ActivatedRoute" and how difficult it will be for your code, I think it will help other people who have this problem when they reach your question, for example, I :).

This question will answer your question.

This code is below (inserted from the above topic) where the "magic" happens. If you put your "reload" code in this subsribe function, your component will reload.

 ngOnInit() { this.sub = this.route.params.subscribe(params => { this.id = +params['id']; // (+) converts string 'id' to a number // Some 'reload' coding // In a real app: dispatch action to load the details here. }); } 
+2
source share

An easy way to do this is to force the template to reboot with * ngIf. You will need a variable that turns the component off and on. If the values โ€‹โ€‹of the variables were changed before this shutdown / on, they will be displayed during the second review.

+1
source share

All Articles