【问题标题】:Not getting same results after refreshing the page刷新页面后没有得到相同的结果
【发布时间】:2020-01-30 22:58:04
【问题描述】:

我想在刷新页面后显示相同的搜索结果。例如,我通过 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 来完成,但无法得到我想要的。有人可以告诉我我做错了什么吗?即使在刷新页面后如何显示相同的结果?

【问题讨论】:

    标签: angular rest crud


    【解决方案1】:

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

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

    【讨论】:

    • 我想过使用本地存储,也发现了一篇文章,但不了解如何在我的代码中实现它..仍在尝试这样做...
    【解决方案2】:

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

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

    然后是 onInit:

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

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 2019-04-21
      • 2015-07-21
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      相关资源
      最近更新 更多