angulartypescripturlhttp-redirectreferrer

proper way to redirect user to their origin page?


I have an application (B) that is linked in another (A). Basically the user is in A and they can click into B.

I'm saving this URL/URI link in a service. This is what I'm doing in the redirect service:

export class DirectService {
  private redirect_uri: any;
  constructor() {}

  setRedirectUri(userUri): void {
    this.redirect_uri = userUri;
  }
  getRedirectUri() {
    return this.redirect_uri;
  }

I'm setting redirect_uri in my container when its initialized. this.redirectService.setRedirectUri(document.referrer); according to the docs this should save the page A that links B

I have button that when clicked will do perform this function:

onClose(): void {
    window.location.href(this.redirectService.getRedirectUri());
  }

I'm getting an error on the href saying this: This expression is not callable. Type 'String' has no call signatures

I've also tried using router like this:

this.router.navigate(this.redirectService.getRedirectUri()) but that gives me this error:

*main.js:1 ERROR TypeError: J.reduce is not a function*
    *at ee (main.js:1:2481641)*
    *at At (main.js:1:2480222)*

What am I doing wrong here?


Solution

  • The first error you are seeing is pointing out the fact that window.location.href isn't a function, it's a string. Simply assign to it like so:

    window.location.href = this.redirectService.getRedirectUri();
    // or just
    window.location = this.redirectService.getRedirectUri();
    

    See https://developer.mozilla.org/en-US/docs/Web/API/Window/location for further reference.