ngrx 效果抛出“调度无效操作:未定义”

daz*_*zed 6 rxjs ngrx ngrx-effects angular

在对话响应的条件下,我对注销确认有这种效果,但出现以下错误:

错误错误:效果“AuthEffects.logoutConfirmation$”调度了一个无效的动作:未定义

错误类型错误:操作必须是对象

效果如下:

@Effect()
logoutConfirmation$ = this.actions$
    .ofType<Logout>(AuthActionTypes.Logout)
    .pipe(
      map(action => {
        if (action.confirmationDialog) {
          this.dialogService
            .open(LogoutPromptComponent)
            .afterClosed()
            .pipe(
              map(confirmed => {
                if (confirmed) {
                  return new LogoutConfirmed();
                } else {
                  return new LogoutCancelled();
                }
              })
            );
        } else {
          return new LogoutConfirmed();
        }
      })
    );
Run Code Online (Sandbox Code Playgroud)

它在激活确认对话框时起作用,我想这是对话框响应的地图有问题,一直试图理解它但找不到方法。任何人都有这方面的线索?

Jot*_*edo 9

您的外部映射应该是 a mergeMap,因为您要将操作映射到新流(如果条件为真)。

您可以按如下方式修复此问题:

import { of } from 'rxjs';
import {map, mergeMap } from 'rxjs/operators';

@Effect()
logoutConfirmation$: Observable<Action> = this.actions$
    .ofType<Logout>(AuthActionTypes.Logout)
    .pipe(
      mergeMap(action => {
        if (action.confirmationDialog) {
          return this.dialogService
            .open(LogoutPromptComponent)
            .afterClosed()
            .pipe(
              map(confirmed => confirmed ? new LogoutConfirmed():new LogoutCancelled())
            );
        } else {
          return of(new LogoutConfirmed());
        }
      })
    );
Run Code Online (Sandbox Code Playgroud)

作为旁注,始终声明您的效果的显式类型,以便在编译时而不是运行时出现错误。

  • 声明显式效果类型以在编译时获取错误是什么意思? (2认同)

Ste*_*ero 6

在我的情况下,我没有发送任何东西,所以把我的问题放在dispatch:false里面@effect()

@Effect({dispatch: false})