【问题标题】:Caching And Refreshing of Data in Angular在 Angular 中缓存和刷新数据
【发布时间】:2018-03-27 10:52:26
【问题描述】:

我在我的 Angular 应用程序中遇到了这个问题,因为我正在缓存数据。在我使用 onCreate() 函数添加内容之后。我在成功订阅函数中调用了 getAll() 函数以获取新数据。但是,我仍然没有得到新的数据。我相信这是因为我的服务中的缓存数据。我如何知道是否有新数据或如何修复我的缓存现有数据并在有新数据时刷新数据?

服务

getAll() {
    if(!this.materials) {
        this.materials = this.httpClient.get<any>(this.url)
                            .map((response => response))   
                            .publishReplay(1)
                            .refCount();

    }
    return this.materials;
  }

getAll() ts

getAllMaterials() {
    this.subscription = this.materialsService.getAll()
        .subscribe(
          (data:any) => {
            this.materials = data.materials;
            console.log(data);
          },
          error => {
           alert("Error");
           console.log(error);
          });
  }

onCreate() -ts

 onCreateMaterial(form: NgForm){

    const formData = {
      sku: form.value.sku,
      name: form.value.name,
      supplier_id: form.value.supplier,
      price: form.value.price
    }

    this.materialsService.addMaterial(formData)
      .subscribe(
          data => {
            let message = data.message;
            alert(message);
            console.log(data);
            this.modalRef.close();
            this.getAllMaterials();
          },
          error => {
             alert("Error Adding");
             console.log(error);
          });
  }

【问题讨论】:

    标签: angular rxjs angular-services angular-forms angular-cache


    【解决方案1】:

    .publishReplay().refCount()you can read it here.做了一些研究,我认为问题在于您在订阅之前使用.publishReplay().refCount(),以便在一次迭代后将主题标记为已完成,因此它永远不会再次触发。 Read about it here.我自己对此并不熟悉,但值得一试。

    如果不是那尝试去掉publishReplay的参数,尝试手动退订看看有没有结果。

    【讨论】:

      猜你喜欢
      • 2012-01-29
      • 2012-06-21
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      相关资源
      最近更新 更多