Crocsx
Crocsx

Reputation: 7589

pass URL in an URL as parameter in angular

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.

Upvotes: 0

Views: 394

Answers (1)

iamaword
iamaword

Reputation: 1499

I'd do something with query params:

currentUrl = this.router.url;
this.router.navigate( ['not-found'], { queryParams: { previousUrl: currentUrl}});

Upvotes: 1

Related Questions