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

Angular升级:将@Effect代码转换为createEffect的实现方案

旧@Effect代码转createEffect实现

以下是将你提供的@Effect代码转换为NgRx createEffect 形式的实现:

import { createEffect, Actions, ofType } from '@ngrx/effects';
import { switchMap, map, catchError } from 'rxjs/operators';
import { of } from 'rxjs';
// 确保导入对应的action类/创建器及服务

getTasks$ = createEffect(() => 
  this.actions$.pipe(
    ofType(taskActions.LOAD_ACCOUNTS_TASKS),
    switchMap(() => {
      return this.rest.get(`${tasksUrl}`).pipe(
        map((response) => new taskActions.LoadTasksSuccess(response)),
        catchError((err: HttpErrorResponse) => of(
          err.status === 401 
            ? new oauthActions.FailedRequestWith401(new taskActions.LoadTasks()) 
            : new taskActions.LoadTasksFail()
        ))
      );
    })
  )
);

关键改动说明:

  • 移除@Effect()装饰器,改用createEffect函数创建副作用
  • createEffect接收一个返回Observable的箭头函数作为参数,内部逻辑与原代码保持一致
  • 若你的项目已使用NgRx 8+推荐的action creator写法(通过createAction定义),可将new taskActions.LoadTasksSuccess(response)替换为taskActions.loadTasksSuccess(response)这类调用方式,代码逻辑不受影响

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 20:18:12