我对ngrx特效很熟悉,我正在用ngrx商店构建angular 5应用程序。我想将一个操作从第一个操作分派到第二个操作,即我有两个操作getAlertFilters和getAlertArticles。
我想发送一个从
GetAlertFilters根据来自HTTP新闻服务的响应执行操作
getfilters函数。我需要使用相同的有效载荷
getAlertFilters操作,需要作为新的负载传递
GeAltReToBox操作的其他参数。
另外,如何从getfilters构造新的对象articlerequest
响应并将其作为有效负载传递给getAlertArticles操作。拜托
教我怎么做。下面是我的代码。
@Effect()
getAlertArticles$ = this.actions$
.ofType(NewsActions.GET_ALERT_ARTICLES_REQUEST)
.pipe(
map((action: NewsActions.GetAlertArticlesRequest) => action.payload),
switchMap((articlesRequest: ArticlesRequest ) => {
return this.newsService.getArticles(articlesRequest.channel, articlesRequest.filter, articlesRequest.locale, articlesRequest.page)
.pipe(
tap(() => this.store.dispatch(new LoaderActions.HideLoader(config.LOADER_ID))),
map(res => {
return {
type: NewsActions.GET_ALERT_ARTICLES_SUCCESS,
payload: res
};
}),
catchError((e: HttpErrorResponse) => Observable.of({
type: NewsActions.GET_ALERT_ERROR,
payload: e
}))
);
}),
);
@Effect()
getAlertFilters$ = this.actions$
.ofType(NewsActions.GET_ALERT_FILTER_REQUEST)
.pipe(
map((action: NewsActions.GetAlertFilterRequest) => action.payload),
switchMap((filterRequest: FiltersRequest ) => {
return this.newsService.getFilters(filterRequest.channel, filterRequest.locale)
.pipe(
tap((res) => {
this.store.dispatch(new NewsActions.GetAlertArticlesRequest({channel: res.getChannel, filter: 768, locale: 'en_ca' , page: 1}));
}),
map(res => {
return {
type: NewsActions.GET_ALERT_FILTER_SUCCESS,
payload: res
};
}),
catchError((e: HttpErrorResponse) => Observable.of({
type: NewsActions.GET_ALERT_ERROR,
payload: e
}))
);
}),
;
最佳答案
我想你可以这样做:
合并两个操作:NewsActions.GET_ALERT_ARTICLES_REQUEST
和NewsActions.GET_ALERT_FILTER_REQUEST
为一个,可能NewsActions.GET_ALERT_FILTER_AND_ARTICLES_REQUEST
你的两个效果现在收听相同的操作NewsActions.GET_ALERT_FILTER_AND_ARTICLES_REQUEST
当您发送NewsActions.GET_ALERT_FILTER_SUCCESS
操作时,您应该将getAlertArticles$
效果所需的参数保存到存储区。
在getAlertArticles$
效果中,在.ofType(NewsActions.GET_ALERT_FILTER_AND_ARTICLES_REQUEST)
之后添加以下链:
代码如下:
getAlertArticles$ = this.actions$
.ofType(NewsActions.GET_ALERT_FILTER_AND_ARTICLES_REQUEST)
.combineLatest(this.store.select(fromNews.getYourNeededData)
.filter(data => !isEmpty(data))
.pipe(
switchMap(([action, data]: [NewsActions.GetAlertFilterAndArticlesRequest, YourDataFormat]) => //do your request here),
关于typescript - 如何在ngrx效果中使用以前的http调用的有效负载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51129007/