how to refresh page in angular 2
If you want to reload the page , you can easily go to your component then do :
location.reload();
Just in case someone else encounters this problem. You need to call
window.location.reload()
And you cannot call this from a expression. If you want to call this from a click event you need to put this on a function:
(click)="realodPage()"
And simply define the function:
reloadPage() {
window.location.reload();
}
If you are changing the route, it might not work because the click event seems to happen before the route changes. A very dirty solution is just to add a small delay
reloadPage() {
setTimeout(()=>{
window.location.reload();
}, 100);
}
Updated
How to implement page refresh in Angular 2+ note this is done within your component:
location.reload();
The simplest possible solution I found was:
In your markup:
<a [href]="location.path()">Reload</a>
and in your component typescript file:
constructor(
private location: Location
) { }
Without a bit more code ... its hard to say what's going on.
But if your code looks something like this:
<li routerLinkActive="active">
<a [routerLink]="/categories"><p>Products Categories</p></a>
</li>
...
<router-outlet></router-outlet>
<myComponentA></myComponentA>
<myComponentB></myComponentB>
Then clicking on the router link will route to the categories route and display its template in the router outlet.
Hiding and showing the child components don't affect what is displayed in the router outlet.
So if you click the link again, the categories route is already displayed in the router outlet and it won't display/re-initialize again.
If you could be a bit more specific about what you are trying to do, we could provide more specific suggestions for you. :-)