ngrx 效果抛出 "dispatched an invalid action: undefined"
ngrx effect throws "dispatched an invalid action: undefined"
我在对话框响应的情况下对注销确认有这种效果,但我收到以下错误:
ERROR Error: Effect "AuthEffects.logoutConfirmation$" dispatched an invalid action: undefined
和
ERROR TypeError: Actions must be objects
效果如下:
@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();
}
})
);
它在确认对话框被激活时起作用,我猜是对话框响应的映射有问题,一直试图理解它但找不到办法。
有人对此有线索吗?
您的外部映射应该是 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());
}
})
);
附带说明,始终声明效果的显式类型,以便在编译时而不是 运行 时出现错误。
就我而言,我没有发送任何东西,所以将 dispatch:false
放在 @effect()
中解决了我的问题。
@Effect({dispatch: false})
当您不在 reducer 中调用此操作类型时会发生这种情况,您需要在您的 reducer 中放置与您在效果中使用的相同操作类型的类似内容:
case AuthActionTypes.Logout:
return {
...state
};
@Stephen Romero 的答案会起作用,但它会忽略调度新值
我在对话框响应的情况下对注销确认有这种效果,但我收到以下错误:
ERROR Error: Effect "AuthEffects.logoutConfirmation$" dispatched an invalid action: undefined
和
ERROR TypeError: Actions must be objects
效果如下:
@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();
}
})
);
它在确认对话框被激活时起作用,我猜是对话框响应的映射有问题,一直试图理解它但找不到办法。 有人对此有线索吗?
您的外部映射应该是 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());
}
})
);
附带说明,始终声明效果的显式类型,以便在编译时而不是 运行 时出现错误。
就我而言,我没有发送任何东西,所以将 dispatch:false
放在 @effect()
中解决了我的问题。
@Effect({dispatch: false})
当您不在 reducer 中调用此操作类型时会发生这种情况,您需要在您的 reducer 中放置与您在效果中使用的相同操作类型的类似内容:
case AuthActionTypes.Logout:
return {
...state
};
@Stephen Romero 的答案会起作用,但它会忽略调度新值