Current route list not updated if already stay in same route in Angular 7?

天涯浪子 提交于 2020-01-06 04:40:29

问题


I have two component header and searList component.

Case 1: Inside header component we have call API for get result after result is get and click search all result link.after click user is redirect to searchList component in which we have showing list of result.

Case 2:

Now user is stay on searList component, now again user is search another user and we get different result now.in such scenario user is not redirect on searhList component because we are already stay on searhList component.

Solution 1: i have tried if user is stay on a same url and we have call function from another component in our class function is called but data is not updated.i don't understand what the problem behind that.

//Component 1st: 
searchResultAfterEnter(){
  //set flag    
  this.isEnter            = true;
  this.isShowSearchBox    = false;
  localStorage.setItem('searchKey', this.currentSearchValue);

  this.currentSearchValue = ''; 

  if(this.router.url === '/pages/search-detail') {
  
       //Here we will reset the curret router with routeReuseStrategy
        //and found solution and worked for me...
       this.router.routeReuseStrategy.shouldReuseRoute = function(){
          return false;
       }

     this.router.events.subscribe((evt) => {
        if (evt instanceof NavigationEnd) {
           this.router.navigated = false;
        }
    });
    
      //get value from anotehr page via key wise,....
  this.search.getResultViaKey(localStorage.getItem('searchKey'));
  } else {
  this.router.navigate(['/pages/search-detail']);
  }
 }
 
 
 //Component 2nd:
getResultViaKey(searchValue : string ) {  
  //pass the dataparam in the backend....
  var data = {
    "search_key"  : <String>searchValue,
    "search_type" : "3",
    "user_id"     : <String>this.loggedUserObject.user_id
  }

  if(this.currentSearchValue.length >= 3){  
    this.loggedUserObject  = JSON.parse(localStorage.getItem('user-detail'));

    //set API URL here....
    var url =  '/api/common-search';
     self.service.saveDetail(value, url).subscribe((data : any) => {
        if(data.status) {
          self.searchResultList =  data.result;
        } else {
          self.searchResultList  = [];
        }
      });
      } 
  }

来源:https://stackoverflow.com/questions/57270195/current-route-list-not-updated-if-already-stay-in-same-route-in-angular-7

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