How to cache view or maintain view state in history back in Angular 2

I am using angular 2 with a designer list of materials with infinite scrolling , and when I select an item from the list, I move on to a detailed view representing: id in the url. If I go back, the list view will reload and the original state will be lost.

What is the correct way to save view state when navigating? Is it possible to cache a view as it is before starting navigation?

+4
source share
2 answers

-, , , "view-state" . , . , . .

ViewStateService . , viewState - . , , . , , .

@Injectable()
export class ViewStateService  {
  viewState : Map<string, Map<string, string> > = new Map<string, Map<string, string> >();
  constructor(){
  }

  setViewState(key : string , map : Map<string, string>){
    this.viewState.set(key, map);
  }

  getViewState(key : string) : Map<string, string>{
    return this.viewState.get(key);
  }
}

. ngOnInit() :

this.viewState = this.viewStateServices.getViewState("ComponentName");

(, , , viewState). OnInit OnDestroy. ngOnInit() - . ngOnDestroy() , . viewstate - (). , viewstate. , - , . , .

Angular , , . , . ( , ), . , .

+3

, .

, / init. . , .

+2

Source: https://habr.com/ru/post/1650482/


All Articles