刷新页面后没有得到相同的结果

Not getting same results after refreshing the page

我想在刷新页面后显示相同的搜索结果。例如,我通过 get 请求提供年、周、页、分支 (2017,17,01, R) 参数来搜索传单。当我刷新页面时,我会看到 ngOnInIt 中的参数。

我想根据我的搜索参数显示最新结果,而不是 ngOnInIt() 中的内容。下面是我的组件。

  ngOnInit() {
    if (this.showRaster === true) {
      this.jahr = this.year;
      this.woche = this.week;
      this.seite = this.page;
      this.filiale = this.branch;
      this.flyerhammService.getFlyer(this.branch, this.year, this.woche, this.seite)
        .then(
          flyers => {
            this.flyers = flyers;
            this.raster = this.flyers[0].Raster;
          }
        ).catch(
          error => console.log(error)
        );
    } else {
      // Below code is to get a flyer with current values of week and year and seite will always be 01

      this.jahr = (new Date()).getFullYear();
      this.currentWeek = String((this.currentWeekNumber()));
      if (this.currentWeek.length < 2) {
        this.woche = '0' + this.currentWeek;
      } else {
        this.woche = this.currentWeek;
      }
      this.seite = '01';
      this.filiale = 'H';
      this.flyerhammService.getFlyer(this.filiale, this.jahr, this.woche, this.seite)
        .then(
          flyers => {
            this.flyers = flyers;
            this.raster = this.flyers[0].Raster;
          }
        ).catch(
          error => console.log(error)
        );
    }
  }

我正在尝试使用 afterViewInit 执行此操作,但无法获得我想要的结果。谁能告诉我我哪里做错了以及如何在刷新页面后显示相同的结果?

您需要将当前搜索参数(状态)存储到浏览器的存储空间(SessionStorage 或 LocalStorage),以便在页面刷新后恢复任何内容。

这更多是因为您正在处理单页应用程序,而不是 Angular 具体。

您需要使用本地存储设置和获取数据: 设置字段时使用以下内容:

localStorage.setItem('jahr', this.jahr);

然后 onInit:

let jahr = localStorage.getItem('jahr');
if (jahr) {
// get your data
}