0

I have recently stared Angular and while learning on how we can navigate using Routes I had an issue on how to send Objects (not just strings) over routes, then I've came across NavigationExtras.state. I'm trying to send a query parameter as key-value pair and it was working fine. I was passing the query to the state and navigating using navigateByUrl and at the component, I'm retrieving the data from paramMap

this.router.navigateByUrl('/search', {state: {searchQuery: this.query}});

At the component:

ngOnInit() {
  this.activatedRoute.paramMap.pipe(
        map(() => window.history.state)
      ).subscribe(result => {
        console.log('searchComponent: ' + result.searchQuery);
        this.query = result.searchQuery;
      });
  }

Now the problem I'm facing is when I try to pass in a new query while I'm on the same route (that is '/search') it's not working. I've read about it online and came to know that if activeRoute doesn't change then the page/component won't be loaded again and we can force reload the component, but in my case, It is entering in an infinite loop call on the API. Is there a way to get the data from the State without reloading the component and make a new service call with that query to fetch the results? Can anyone help me understand how we can approach this issue?

Thanks

Edit: Here I'm using NavigationExtras.state because my data is not necessarily a key-value pair it can also be an object(for now it is just a simple query so it's just a key-value pair it can change in future so I'm using state).

Everything works fine the first time, what I mean is that I'm able to send data through state and get it at the search-Component and perform the search operation to retrieve the data from the server. So here I have the search input where I'm entering my query. Once I perform the search Operation my page is navigated to /search so when I try to enter another query to do the same again while I'm still on /search it won't work because If your navigate() doesn't change the URL that already shown on the address bar of your browser, the router has nothing to do. So what I'm trying now is how can I refresh the component/page if the query changes and also is there a way to get the new data without refreshing the component.

Edit 2:

this.navigationSubscription = this.router.events.subscribe((e: any) => {
      console.log("instance of e", e)
      if (e instanceof NavigationEnd) {
        this.initialiseSearch();
       }
    }); 

After the changes from the solution @Andrew mentioned in the comments, I'm having issues at the first time of loading the component. From the second time the component is reloading and it's working.

Edit 3: I had to move the above code (this.navigationSubscription) from ngOnInit() to the constructor as ngOnInit() gets called only once.

Srikar
  • 351
  • 5
  • 16
  • You passed a query to the`/search` route, then you want to pass an extra query from within the `/search` route? Is that it? – eMontielG Jan 31 '20 at 18:03
  • Also, is there a reason as to why you're using paramMaps and not queryParams for key:value pair data? – eMontielG Jan 31 '20 at 19:30
  • 1
    Try this link on how to reload the page on same route: https://stackoverflow.com/a/48280010/9986654 – Andrew Jan 31 '20 at 22:17
  • @eMontielG I'm making search API call with the query that I'm passing in the NavigationExtras.state. There is no specific reason for not using queryParams. I'm trying to send data through NavigationExtras.state so to get the data I need to go to window.history.state to get the data so I need to use paramMaps . That's what I've read, not sure if there is any other way to do it. – Srikar Feb 01 '20 at 03:24
  • I'm still not sure what you actually need, sorry. I made a [quick demo](https://stackblitz.com/edit/angular-93fja4) of what I think you're after, does any of it help? – eMontielG Feb 01 '20 at 04:58
  • @eMontielG thank you for making a quick demo to help me, What I'm actually trying to do here is I am using NavigationExtras.state to send data (Here my data is a key-value pair named as **{state: {searchQuery: this.query}}**) but my data is not necessarily a key-value pair that's why I'm using NavigationExtras.state. The problem I have is that If your navigate() doesn't change the URL that already shown on the address bar of your browser, the router has nothing to do. That's why the query is not changing. I'm trying out the link Andrew shared above. Thank you very much for your effort :) – Srikar Feb 01 '20 at 05:24
  • @Andrew the link you mentioned above worked but the problem I'm facing now is for the first time I go to that route it is not working. Once I'm on /search it is reloading the component and I can get the new query. but as I'm trying to filter the router.event to NavigationEnd, when I'm trying to debug it's not even going into the subscribe the first time. Can you please help me with this? I've added the code above in the question. Please do have a look. Thank you – Srikar Feb 02 '20 at 10:05
  • I'm confused what you mean by the first time you go to the route, do you mean it simply doesn't load? What route are you referring to? – Andrew Feb 02 '20 at 20:22
  • @Andrew Here is a demo application I've created [demo](https://stackblitz.com/edit/angular-wiaxwg) of the issue I'm facing please have a look at it. The problem is once I type the query in the search and press the button it is not calling my **initialiseSearch** method. I think that's because there is no nothing available yet to subscribe. Then once you are in **/search** then type another query in the search and press the button and it works (the component is reloading). So for the first query (when coming from home to search), it's not working but 2nd (from search to search) it's working – Srikar Feb 03 '20 at 16:48

0 Answers0