[英]How to get the value from ngrx store
I have an angular 8 project and I am using ngrx
to store an array of objects in a state store. 我有一个angular 8项目,我正在使用
ngrx
在状态存储中存储对象数组。 I managed to do that, but now, I want to make a post request
to the server with the data that is in the state store. 我设法做到了,但是现在,我想使用状态存储中的数据向服务器发出
post request
。
this.dataService.postData(this.store.pipe(select('dataStore')));
That is the code I am trying and it is not working. 那是我正在尝试的代码,它不起作用。 Thank you
谢谢
Looks like you are trying to perform side effect. 看起来您正在尝试执行副作用。 To do that you should do the following in your component/service -
为此,您应该在组件/服务中执行以下操作-
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);
});
BTW - To handle the side effect you should try ngrx Effects - https://ngrx.io/guide/effects 顺便说一句-要处理的副作用,你应该尝试NGRX影响- https://ngrx.io/guide/effects
Try the following 尝试以下
import { createFeatureSelector } from '@ngrx/store';
const dataSelector = createFeatureSelector('dataStore');
this.store.select(dataSelector).subscribe(
(data) => {
this.dataService.postData(data)
}
);
Simplest Answer 最简单的答案
this.store.pipe(select('dataStore'), take(1)).subscribe((data) => {
this.dataService.postData(data).subscribe();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.