In Angular 2, is there a way to navigate and pass data that is not shown in the URL?
I want to create a component that receives a complex object, but do not want this to be displayed in the URL.
Thanks!
Only to support new readers who are using new versions of angular, in angular 7.2 and above you can use state to pass data objects dynamically without showing in url.
this.router.navigate(['/some-route'], {state: {data: {...}}});
then you can read state like:
ngOnInit() {
this.state = this.activatedRoute.paramMap
.pipe(map(() => window.history.state));}
If you are using navigate
method, then add skipLocationChange
like this:
this.router.navigate(['/your-path'], { skipLocationChange: true });
If you're using routerLink
, use it as an attribute like this:
<a [routerLink]="['/your-path']" skipLocationChange></a>
Please bear in mind that if you use skipLocationChange
, the route in the browser won't be updated after navigating to the new route, so if the user hits back in the browser he'll be sent to the path before the first path. I haven't found a solution for this yet.
You can use below function to pass parameters:
this.router.navigate(['/somePage'], {queryParams: {'name': 'Emon'}, skipLocationChange: true});
This works for me!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With