Wellington Silva
Wellington Silva

Reputation: 11

Is it possible to redirect only the query params of a route not found?

The application will receive a redirect from an authentication provider, but without hash in the url.

I need redirect the params from http://host/?param1=123 to http://host/#/login?param1=123

How configure routes for this?

{ path: '', redirectTo: '/login', pathMatch: 'full'},
{ path: 'login', component: LoginComponent },

Upvotes: 1

Views: 1323

Answers (2)

Ben Racicot
Ben Racicot

Reputation: 5895

Does it have to be done within the route paths?

Programmatically I get the params for reference like this:

this.parameters = Object.assign({}, this.route.snapshot.parent.queryParams);

Now you can redirect with this.parameters or build them up within navigate queryParams argument

this.router.navigate(['/profile/3'], { queryParams: { position: pos, 'salary': 'expensive' } }); // add .then(() => {}) for promise

For redirection

this.router.navigate(['login'], { preserveQueryParams: true });

Template HTML routing

<a [routerLink]="['/profile/3']" [queryParams]="{position:'boss', 'salary': 'expensive' }">

If you need more, let me know and I will help you.

Upvotes: 1

Amir Arbabian
Amir Arbabian

Reputation: 3699

It's not possible with current angular route config redirectTo functionality.

What you can do is to write a [CanActivate] guard, put it on that empty route '', and inside of guard redirect user to /login preserving query params using Router or returning UrlTree from guard.

For example, inside of guard it could look like (using Router):

  ...
  this.router.navigate(['login'], { preserveQueryParams: true });
  ...

Hope that helps.

Upvotes: 0

Related Questions