Search code examples
angulartypescriptquery-string

angular how to refresh the page without losing the query string they pass to me


I'm creating a small booking app, where you don't need to register. I created a method to take the querystring parameter and at startup (ngOinit) it recognizes the parameter as well as the user ID and loads all the appointments taken by the same user id. the problem is that when the reservation was made I inserted a button that closes the confirmation dialog and refreshes the page, after the refresh the page the query string no longer appears and I automatically lose the ability to capture the user ID! could you tell me how i can refresh the page without losing the user ID?

method for capturing the query:

public getQueryParameter(key: string): string {
    const parameters = new URLSearchParams(window.location.search);
    return parameters.get(key);

method that closes the dialogue and reloads the page

  click() {
    const devCode = this.getQueryCode.getQueryParameter('d');
    this.shareDate.device.deviceId = devCode;
    window.location.reload();
}

thanks a lot to everyone


Solution

  • Instead of doing window.location.reload (which I'd argue is an anti-pattern to angular), you can do this within the router and preserve the query string with Navigation Extras

    this.router.navigate(['/'], { queryParamsHandling: "preserve"});