Angular ngrx效应抛出;已调度无效操作:未定义";

Angular ngrx效应抛出;已调度无效操作:未定义";,angular,rxjs,ngrx,ngrx-effects,Angular,Rxjs,Ngrx,Ngrx Effects,在对话框响应的情况下,我对注销确认具有此效果,但我收到以下错误: 错误:Effects“AuthEffects.logoutConfirmation$”调度了一个无效的操作:未定义 及 错误类型错误:操作必须是对象 大意如下: @Effect() logoutConfirmation$ = this.actions$ .ofType<Logout>(AuthActionTypes.Logout) .pipe( map(action => {

在对话框响应的情况下,我对注销确认具有此效果,但我收到以下错误:

错误:Effects“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();
        }
      })
    );
@Effect()
logoutConfirmation$=this.actions$
.of类型(AuthActionTypes.Logout)
.烟斗(
映射(操作=>{
if(action.confirmationDialog){
这是dialogService
.打开(LogoutPromptComponent)
.afterClosed()
.烟斗(
地图(已确认=>{
如果(已确认){
返回新的logoutconfirm();
}否则{
返回新LogoutCancelled();
}
})
);
}否则{
返回新的logoutconfirm();
}
})
);
当确认对话框被激活时,它就工作了,我猜是对话框的响应图出了问题,我一直在试图理解它,但找不到办法。
有人对此有线索吗?

您的外部映射应该是一个
mergeMap
,因为您正在将操作映射到一个新流(如果条件为true)

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

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());
        }
      })
    );
从'rxjs'导入{of};
从“rxjs/operators”导入{map,mergeMap};
@效果()
logoutConfirmation$:Observable=this.actions$
.of类型(AuthActionTypes.Logout)
.烟斗(
合并映射(操作=>{
if(action.confirmationDialog){
返回此对话框服务
.打开(LogoutPromptComponent)
.afterClosed()
.烟斗(
映射(已确认=>已确认?新注销已确认():新注销已取消()
);
}否则{
返回(新登录已确认());
}
})
);

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

在我的情况下,我没有调度任何东西,因此将
调度:false
放入
@effect()
修复了我的问题

@效果({dispatch:false})


当您不在减速器中调用此动作类型时,会发生这种情况。您需要在减速器中使用与您在效果中使用的动作类型相同的类似内容:

case AuthActionTypes.Logout:
            return {
                ...state
            };

@Stephen Romero answer将正常工作,但它将忽略发送新值

这是运行时错误还是编译时错误?否,是在发送操作时发生的。请尝试添加
返回this.dialogService//etc.
获得效果“AuthEffects.logoutConfirmation$”发送了无效操作:[object]并且操作必须具有类型属性将
logoutConfirmation$
的显式类型声明为
logoutConfirmation$:Observable
并查看发生了什么;)