2017-10-16 212 views
0

我在我的角度應用程序中有這個問題,因爲我正在緩存數據。使用onCreate()函數添加內容後。我在successfull subscribe函數中調用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); 
      }); 
    } 

回答

0

難道就.publishReplay().refCount()you can read it here.一些研究,我認爲這個問題是您正在使用.publishReplay().refCount()您訂閱過,所以,作爲一個迭代後完成主題標記,所以它絕不會再次觸發。 Read about it here.我自己對此並不熟悉,但值得一試。

如果不是嘗試刪除publishReplay的參數並嘗試手動取消訂閱以查看它是否有任何結果。