0

I am building a 404 page, and I would like to be redirected to different location depending on where the user was.

I made a route in my router that looks like this :

      {
        path: 'not-found/:backTo,
        component: NotFoundComponent,
      },

in my code I do

      this.router.navigate(
        [
          'not-found',
          this.router.serializeUrl(
            this.router.createUrlTree(
              ['new', 'url', 'for', 'redirect']
            )
          ),
        ],
        { skipLocationChange: true }
      )

so it looks like navigate to this.router.navigate(["not-found", "/new/url/for/redirect"])

problem is, I never arrive to my 404 page, which was working before I tried this.

I guess I am doing wrong, cause the url is wierd, and it should be like

not-found?backTo='new/url/for/redirect'

but I can't manage to make it, and work with angular routing.

1 Answer 1

1

I'd do something with query params:

currentUrl = this.router.url;
this.router.navigate( ['not-found'], { queryParams: { previousUrl: currentUrl}});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.