Skip to content

Go Back with URL Parameter

routing.service.ts

ts
export class RoutingService {
  public readonly currentUrlParameter = signal<string>('');
  public readonly previousUrlParameter = signal<string>('');
  // todo: use injectable service for url encoding
  private _urlSerializerService = new HttpUrlEncodingCodec();

//  ...

  constructor() {
    super();

    const _currentParameter = this.url.split('/').pop() || '';
    const _currentGID =
      this._urlSerializerService.decodeValue(_currentParameter);
    this.currentUrlParameter.set(_currentGID);

    this.events.subscribe((event) => {
      if (event instanceof NavigationEnd) {
        this.previousUrlParameter.set(this.currentUrlParameter());

        const _currentParameter = event.url.split('/').pop() || '';
        const _currentGID =``
          this._urlSerializerService.decodeValue(_currentParameter);
        this.currentUrlParameter.set(_currentGID);
      }
    });
  }
}

mycomponent.component.ts

ts

export class MyComponent {
  private router = inject(RoutingService);


  goBack() {
    console.log(this.router.previousUrlParameter());
    console.log(this.router.currentUrlParameter());
  }
}

References