Angular 6 - navigation to child route refreshes whole page
So I found a working solution, which while not very elegant, it... works. In my parent component I created a method like this one:
constructor(public route: ActivatedRoute, private router: Router) {
this.loading = true;
this.param1 = this.route.snapshot.params.param1;
this.param2 = this.route.snapshot.params.param2;
// get data
}
navigateToChild(param3: string) {
this.router.navigate([param3], { relativeTo: this.route });
}
And in the parent template, I did this:
<a (click)="navigateToChild(paramFromServer)">
<b>Navigate</b>
</a>
No more refreshes for this one.
Thank you for your help everyone.
Angular 6 same router.navigate causes refresh when called from another component
Finally figured out what was causing the refresh. The button with the (click)
handler that was causing the refresh was sitting inside a <form>
tag. Whenever the click function was called which did the router.navigate it caused a form submission which seems to be the cause
Angular 6 : Cannot load child route on page load
If you are using lazy loading you MUST NOT include lazy loaded module as import (nor you can use TS import from that module) because it will be loaded along importing module.
planet_hunter takes a lot of credit here for spotting your imports.
Angular - Navigate to current same URL with component reload not the whole page
I have found this workaround method since the onSameUrlNavigation
property in Angular 5.1 is not what I need.
I just need to override shouldReuseRoute
, so added an exception for the component route that I want to refresh, if the current and future route are equal view
.
And also I can navigate to other routes without problems, even for navigate from Angular Material
sidenav
.
this.router.routeReuseStrategy.shouldReuseRoute = function (future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot) {
if (future.url.toString() === 'view' && curr.url.toString() === future.url.toString()) {
return false;
}
return (future.routeConfig === curr.routeConfig);
};
Update:
For those they want to use the new onSameUrlNavigation
property in Angular 5.1 you can refer to this blog:
https://medium.com/engineering-on-the-incline/reloading-current-route-on-click-angular-5-1a1bfc740ab2
Related Topics
How to Add Spaces to for Loop Output in JavaScript
Why Does Prettier Not Format Code in VS Code
Navigationduplicated Navigating to Current Location ("/Search") Is Not Allowed
Parsing Error: Unexpected Token, Expected ","
How to Pass Django Variable into JavaScript
Uncaught Referenceerror: (Function) Is Not Defined At
How to Set the Multi Sliders in One Page
Php - Return Confirm Within PHP Issue
How to Change Options Based on Another Select Option in a Table
Check Whether Elements from One Array Are Present in Another Array
How to Send a Http Delete Request from Browser
Why Is React Webpack Production Build Showing Blank Page
Get High-Order Block'S Id on Click
How to Put a Variable in an Src Attribute
How to Call a Function When Element Is Loaded At Angular
Angular 6 - Navigation to Child Route Refreshes Whole Page
How to Translate the Following Jquery Code to React
How to Compare Two Objects and Get Key-Value Pairs of Their Differences