You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular NgRx技术问询:如何在Effect中获取ofType指定动作的参数并传递给服务方法

Answer

Absolutely! You can easily access the keywords parameter from your filterArticles action and pass it to getFilteredCardInfo—here's how to adjust your Effect code:

Updated Effect Code

filterArticles$ = createEffect(() => this.actions$.pipe(
  ofType(filterArticles),
  // Capture the action instance to access its keywords property
  switchMap((action) => from(this.cardService.getFilteredCardInfo(action.keywords)).pipe(
    map(articles => loadArticlesSuccess({ articles: articles })),
    catchError(error => of(loadArticlesFailure({ error })))
  ))
));

Key Changes Explained

  • Access the action payload: When you use ofType(filterArticles), the stream emits the actual filterArticles action object. By adding (action) inside switchMap, you can access its properties—including keywords.
  • Pass keywords to the service: Replace the empty string '' with action.keywords to send the user's input directly to getFilteredCardInfo.

Quick Check: Ensure Your Action is Defined Correctly

Make sure your filterArticles action creator is set up to accept the keywords parameter, like this:

import { createAction, props } from '@ngrx/store';

export const filterArticles = createAction(
  '[Articles] Filter Articles',
  props<{ keywords: string }>() // This defines the payload structure
);

That's it! Now when filterArticles is dispatched with a keywords value, your Effect will pick it up and use it to filter your articles via the service.

内容的提问来源于stack exchange,提问作者Yaroslav Omelianenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:42:35