Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload page after second click on routerLink

This question has already asked. When we click the second time on a link like that <a routerLink="/home" routerLinkActive="active">Home</a>, the page does not reload.

Angular2 Router3 - Can't reload/refresh active route

Angular 2 reloads routerLink user clicks again

But it has been solved when angular 2 was in beta version and there was an angular issue regarding the question.

Now that we have angular 4, I will ask if the answers provided are still the best way to solve the issue.

like image 473
edkeveked Avatar asked Dec 14 '22 21:12

edkeveked


1 Answers

I was curious and did some googling on my own. I found this workaround on a GitHub feature request for Angular:

this._router.routeReuseStrategy.shouldReuseRoute = function(){
    return false;
};

this._router.events.subscribe((evt) => {
    if (evt instanceof NavigationEnd) {
        this._router.navigated = false;
        window.scrollTo(0, 0);
    }
});

I tried adding this to my app.component.ts ngOnInit function, and it sure worked. All further clicks on the same link now reloads the component and data.

Link to original GitHub feature request

Credit goes to mihaicux2 on GitHub.

I tested this on version 4.0.0-rc.3 with import { Router, NavigationEnd } from '@angular/router';

like image 147
Arg0n Avatar answered Dec 31 '22 11:12

Arg0n