ChatGPT解决这个技术问题 Extra ChatGPT

Angular: How to update queryParams without changing route

I am trying to update (add, remove) queryParams from a component. In angularJS, it used to be possible thanks to :

$location.search('f', 'filters[]'); // setter
$location.search()['filters[]'];    // getter

I have an app with a list that the user can filter, order, etc and I would like to set in the queryParams of the url all the filters activated so he can copy/paste the url or share it with someone else.

However, I don't want my page to be reloaded each time a filter is selected.

Is this doable with the new router?


P
Phil

You can navigate to the current route with new query params, which will not reload your page, but will update query params.

Something like (in the component):

constructor(private router: Router) { }

public myMethodChangingQueryParams() {
  const queryParams: Params = { myParam: 'myNewValue' };

  this.router.navigate(
    [], 
    {
      relativeTo: activatedRoute,
      queryParams: queryParams, 
      queryParamsHandling: 'merge', // remove to replace all query params by provided
    });
}

Note, that whereas it won't reload the page, it will push a new entry to the browser's history. If you want to replace it in the history instead of adding new value there, you could use { queryParams: queryParams, replaceUrl: true }.

EDIT: As already pointed out in the comments, [] and the relativeTo property was missing in my original example, so it could have changed the route as well, not just query params. The proper this.router.navigate usage will be in this case:

this.router.navigate(
  [], 
  {
    relativeTo: this.activatedRoute,
    queryParams: { myParam: 'myNewValue' },
    queryParamsHandling: 'merge'
  });

Setting the new parameter value to null will remove the param from the URL.


I had to use [] instead of ['.'] to make it work
Need to use queryParams['relativeTo'] = this.activatedRoute; to make the navigation relative to the current page
Nice solution for solving the OP's problem. What it doesn't get you, unless I'm missing something, is the ability to go backward and forward in your history of the current page (e.g. changed filter or order 5 times, each having own URL but same component) and have the page's content appear how it would if you accessed those 5 URLs directly. I think you could manually accomplish this via this.activatedRoute.queryParams.subscribe and do various updates in your component, but is there a simple Angular way to just load the route such that back and forward would work automatically?
this actually destroys the component and calls ngOnit() everytime for change in url param. Is there a way to avoid calling ngOnit() ?
when using router.navigate it will scroll top of the window, any ideas how to disable this?
r
rofrol

@Radosław Roszkowiak's answer is almost right except that relativeTo: this.route is required as below:

constructor(
    private router: Router,
    private route: ActivatedRoute,
) {}

changeQuery() {
    this.router.navigate(['.'], { relativeTo: this.route, queryParams: { ... }});
}

b
bjornalm

In Angular 5 you can easily obtain and modify a copy of the urlTree by parsing the current url. This will include query params and fragments.

  let urlTree = this.router.parseUrl(this.router.url);
  urlTree.queryParams['newParamKey'] = 'newValue';

  this.router.navigateByUrl(urlTree); 

The "correct way" to modify a query parameter is probably with the createUrlTree like below which creates a new UrlTree from the current while letting us modify it using NavigationExtras.

import { Router } from '@angular/router';

constructor(private router: Router) { }

appendAQueryParam() {

  const urlTree = this.router.createUrlTree([], {
    queryParams: { newParamKey: 'newValue' },
    queryParamsHandling: "merge",
    preserveFragment: true });

  this.router.navigateByUrl(urlTree); 
}

In order to remove a query parameter this way you can set it to undefined or null.


navigateByUrl is diffreent to navigate - not just with the UrlTree param. See stackoverflow.com/a/45025432/271012
m
moi_meme

The answer with most vote partially worked for me. The browser url stayed the same but my routerLinkActive was not longer working after navigation.

My solution was to use lotation.go:

import { Component } from "@angular/core";
import { Location } from "@angular/common";
import { HttpParams } from "@angular/common/http";

export class whateverComponent {
  constructor(private readonly location: Location, private readonly router: Router) {}

  addQueryString() {
    const params = new HttpParams();
    params.append("param1", "value1");
    params.append("param2", "value2");
    this.location.go(this.router.url.split("?")[0], params.toString());
  }
}

I used HttpParams to build the query string since I was already using it to send information with httpClient. but you can just build it yourself.

and the this._router.url.split("?")[0], is to remove all previous query string from current url.


This SHOULD be the accepted answer, the router.navigates also refreshes the ngOnit, location.go does NOT do this! This is really important because if your component does some ngOnit logic (HTTP calls etc.) they will get called again when you use router.navigate. You don't want this.
Why do you need router, for url only? Isn't this.location.path().split... better?
@Vladimir you may be right, I was using it for other purpose in my component, so it was not only for that. I do not have time to validate though.
This will not work if you want to preserve query parameters, for instance promotion or google tracking parameters
That is the only answer that worked for me, and it makes sense actually.
e
er-sho

Try

this.router.navigate([], { 
  queryParams: {
    query: value
  }
});

will work for same route navigation other than single quotes.


O
Owen Kelvin

If you want to change query params without change the route. see below example might help you: current route is : /search & Target route is(without reload page) : /search?query=love

    submit(value: string) {
      this.router.navigate( ['.'],  { queryParams: { query: value } })
        .then(_ => this.search(q));
    }
    search(keyword:any) { 
    //do some activity using }

please note : you can use this.router.navigate( ['search'] instead of this.router.navigate( ['.']


I'm not sure that the [.] is a better approach than relativeTo:, but the .then() was helpful - was not aware that navigate() returned a promise, so glad you posted!
Instead of using ['.'] or ['search'], we can simply use []
G
Gaurav Paliwal

I ended up combining urlTree with location.go

const urlTree = this.router.createUrlTree([], {
       relativeTo: this.route,
       queryParams: {
           newParam: myNewParam,
       },
       queryParamsHandling: 'merge',
    });

    this.location.go(urlTree.toString());

Not sure if toString can cause problems, but unfortunately location.go, seems to be string based.


This will not trigger an update to observers of the queryParamMap observable
B
Brachacz

Better yet - just HTML:

<a [routerLink]="[]" [queryParams]="{key: 'value'}">Your Query Params Link</a>

Note the empty array instead of just doing routerLink="" or [routerLink]="''"


Why the empty array? [routerLink]="" works.
B
Bjarne Gerhardt-Pedersen

Angular's Location service should be used when interacting with the browser's URL and not for routing. Thats why we want to use Location service.

Angulars HttpParams are used to create query params. Remember HttpParams are immutable, meaning it has to be chained when creating the values.

At last, using this._location.replaceState to change to URL without reloading the page/route and native js location.path to get the url without params to reset the params every time.

constructor(
    private _location: Location,
) {}

...

updateURLWithNewParamsWithoutReloading() {
    const params = new HttpParams().appendAll({
        price: 100,
        product: 'bag'
    });

    this._location.replaceState(
        location.pathname,
        params.toString()
    );
}

V
VIKAS KOHLI

First, we need to import the router module from angular router and declare its alias name

import { Router } from '@angular/router'; ---> import
class AbcComponent implements OnInit(){
constructor(
    private router: Router ---> decalre alias name
  ) { }
}

1. You can change query params by using "router.navigate" function and pass the query parameters

this.router.navigate([], { queryParams: {_id: "abc", day: "1", name: "dfd"} 
});

It will update query params in the current i.e activated route

The below will redirect to abc page with _id, day and name as query params this.router.navigate(['/abc'], { queryParams: {_id: "abc", day: "1", name: "dfd"} }); It will update query params in the "abc" route along with three query paramters

For fetching query params:-

    import { ActivatedRoute } from '@angular/router'; //import activated routed

    export class ABC implements OnInit {

    constructor(
        private route: ActivatedRoute //declare its alias name
      ) {}

    ngOnInit(){
       console.log(this.route.snapshot.queryParamMap.get('_id')); //this will fetch the query params
    }

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now