问题
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