5

I'm creating a redirect in angular 6

The redirect itself is very simple it works like this

get destination url from params:

this.returnUrl = this.route.snapshot.queryParams['route'] || '/';

Redirect

if (this.returnUrl) {
  this.router.navigate([this.returnUrl]);
} else {
  this.router.navigate(['/']);
}

The problem I have in when the url has parameters in it, for example:

Redirect URL is

'/survey/finish?key=7krmpqpC0P&mind=Akkoord&companyNumber=%5B%5BQ2%5D'

As a result I get the error

Error: Cannot match any routes. URL Segment: 'survey/finish%3Fkey%3D7krmpqpC0P&mind%3DAkkoord&companyNumber%3D%255B%255BQ2%255D'

How can I correctly redirect to the given string?

So http://localhost:4200/survey/finish?key=7krmpqpC0P&mind=Akkoord&companyNumber=%5B%5BQ2%5D

My route looks like thid

  {
    path: 'survey/finish',
    component: CallbackComponent,
    canActivate: [AuthGuard]
  }
3
  • I marked it as duplicate of this question because the issue is the same, and you might find a solution in the original question. If you think this doesn't fit your case, please let me know and explain why. Commented Sep 4, 2018 at 11:24
  • what do your routes look like? Commented Sep 4, 2018 at 11:24
  • There seems to be a similar solution here that worked for me. Commented Jun 24, 2020 at 2:17

1 Answer 1

16

use navigateByUrl method:

if (this.returnUrl) {
  this.router.navigateByUrl(this.returnUrl);
} else {
  this.router.navigateByUrl('/');
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the perfect solution - I wonder why this thread is way underrated ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.