簡體   English   中英

如何從ngrx商店獲取價值

[英]How to get the value from ngrx store

我有一個angular 8項目,我正在使用ngrx在狀態存儲中存儲對象數組。 我設法做到了,但是現在,我想使用狀態存儲中的數據向服務器發出post request

this.dataService.postData(this.store.pipe(select('dataStore')));

那是我正在嘗試的代碼,它不起作用。 謝謝

看起來您正在嘗試執行副作用。 為此,您應該在組件/服務中執行以下操作-

this.store.pipe(select('dataStore'), 
                take(1)
                switchMap((data) => {
                  return this.dataService.postData(data);
                }
               ).subscribe(responseOfDataService => {
                  //do whatever you want to do with the response
                  console.log(responseOfDataService);
               });

順便說一句-要處理的副作用,你應該嘗試NGRX影響- https://ngrx.io/guide/effects

嘗試以下

import { createFeatureSelector } from '@ngrx/store';

const dataSelector = createFeatureSelector('dataStore');

this.store.select(dataSelector).subscribe(
   (data) => {
     this.dataService.postData(data)
   }
);

最簡單的答案

this.store.pipe(select('dataStore'), take(1)).subscribe((data) => {

this.dataService.postData(data).subscribe();

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM