On query parameters change, route is not updating

百般思念 提交于 2019-12-09 21:07:01

问题


In my application, there are multiple links in which I have some links with the same route but with different query parameters.

say, I have links like:

.../deposits-withdrawals
.../deposits-withdrawals?id=1
.../deposits-withdrawals?id=2&num=12321344

When I am in one of the above routes and native to the other route from above mentioned, the route is not changing. Not even any of the functions like ngOnInit or ngOnChanges being called.I have changed the parameters from queryParameters to matrixParameters but with no success. I have gone through many links and answers. But, none of them solved my problem. Help me how to solve this.

Thank you...

EDIT:

<button routerLink="/deposits-withdrawals" [queryParams]="{ type: 'deposit' ,'productId': selectedBalance.ProductId}" class="wallet-btns">DEPOSIT {{selectedBalance.ProductSymbol}}</button>
<button routerLink="/deposits-withdrawals" [queryParams]="{ type: 'withdrawal' ,'productId': selectedBalance.ProductId }" class="wallet-btns">WITHDRAW {{selectedBalance.ProductSymbol}}</button>

回答1:


I had this problem once. Can you put some code, or solutions you tried? I'll give you something working for me, but you better give me some more details so that I can help. Suppposing we are here : _some_url_/deposits-withdrawals and we wish to navigate , changing only parameters.

let url = "id=2&num=12321344"
this.router.navigate(['../', url], { relativeTo: this.route });

Hope it helps :/

=================================== EDIT==================================

You have to detect that query parameters have changed. And for that, you may add a listener to queryParameters changings in the constructor of your component. This can be done using your router this way :

constructor(route:ActivatedRoute) { 
    route.queryParams.subscribe(val => { 
        // put the code from ngOnInit here 
    }); 
}

Adding this listener to detect query parameters changes, means you have to move your code from ngOnInit function to this listener. And every time, you navigate, it will be called.

For navigating, you may use html navigation, or ts navigation. If you want it to be in html, you may use :

<button routerLink="/deposits-withdrawals" [queryParams]="{ type: 'withdrawal' ,'productId': selectedBalance.ProductId }" class="wallet-btns">WITHDRAW {{selectedBalance.ProductSymbol}}</button>



回答2:


The ngOnInit() has to be re-invoked when query param is updated. This can be achieved as follow:

import { Router } from '@angular/router';
constructor(private router: Router) {
    this.router.routeReuseStrategy.shouldReuseRoute = () => false;
}



回答3:


Changing parameters usually won't cause an ngOnInit.

If you are navigating to the same page with different parameter, you can listen to events such as NavigationEnd. based on that you will be able to trigger the functions that you want.

import { Router, NavigationEnd } from '@angular/router';
export class AppComponent {
...
  constructor(public userService: UserService, router:Router) {
    router.events.forEach((event) => {
      if(event instanceof NavigationEnd) {
        console.log(location.pathname);
      }
      //NavigationStart
      // NavigationEnd
      // NavigationCancel
      // NavigationError
      // RoutesRecognized
    });



回答4:


I solved this problem like this.

Suppose you have a container news-list.component.ts with ngOnInit. It saves current queryParams in currentFilters and if there is not them makes simple GET request else it makes POST request.

ngOnInit() {
  this.route.queryParams.subscribe(queryParams => {
    if (!!queryParams) {
      this.currentFilters = <NewsFilter>{...queryParams, offset: 0, size: 6};
      this.news$ = this.newsPostsService.getNewsByFilter(this.currentFilters);
    } else {
      this.news$ = this.newsPostsService.getMainNews();
    }
  });
}

Then you create an component <news-rubric></news-rubric> which has following view. You pass there currentFilters and take rubricClick which you process next.

news-list.component.html

<ml-news-rubrics [currentFilters]="currentFilters"
                 (rubricClicked)="onRubricFilter($event)"
></ml-news-rubrics>

news-list.component.ts

onRubricFilter(filters: NewsFilter) {
  this.currentFilters = {...filters};
  this.router.navigate([], {queryParams: filters, relativeTo: this.route});
}

And then inside news-rubric.component.ts you do something like this:

onRubricClicked(rubricId: string) {
  // check if filter exists and if not then put ID in filter
  if (!this.currentFilters.filterByAnyRubricIds) { 
    this.putIdInFilter('filterByAnyRubricIds', rubricId, this.currentFilters.filterByAnyRubricIds);
  } else {
    // check if clicked ID is not in filter. put in filter
    if (!this.currentFilters.filterByAnyRubricIds.includes(rubricId)) { 
      this.putIdInFilter('filterByAnyRubricIds', rubricId, this.currentFilters.filterByAnyRubricIds);
    } else { 
      // if ID in filter remove it from filter
      this.removeIdFromFilter('filterByAnyRubricIds', rubricId, this.currentFilters.filterByAnyRubricIds);
    }
  }
  this.rubricClicked.emit(this.currentFilters);
}

There is most tricky code. It makes new filter by updating its key with filtered ID.

private putIdInFilter(key: string, value: any, list: any) {
  if (!list || !(list instanceof Array)) {
    if (!list) {
      this.currentFilters = {...this.currentFilters, [key]: [value]};
    } else {
      this.currentFilters = {...this.currentFilters, [key]: [this.currentFilters[key], value]};
    }
  } else {
    this.currentFilters = {...this.currentFilters, [key]: [...this.currentFilters[key], value]};
  }
}

private removeIdFromFilter(key: string, value: any, list: any) {
  if (!list || !(list instanceof Array)) {
    this.currentFilters = <NewsFilter>{
      ...this.currentFilters, [key]: null
    };
    return;
  }
  const filteredValues = [...list.filter(i => i !== value)];
  if (filteredValues.length > 0) {
    this.currentFilters = <NewsFilter>{
      ...this.currentFilters, [key]: filteredValues
    };
  } else {
    delete this.currentFilters[key];
  }
}

And NewsFilter it is merely interface like QueryParams with keys which are required to be filtered.



来源:https://stackoverflow.com/questions/46969864/on-query-parameters-change-route-is-not-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!