当使用 NgRx 数据效果时,它监视SAVE_ADD_ONE_SUCCESS
,添加实体订阅永远不会触发。也许是因为我有dispatch: false
效果?但我并不总是想返回另一个动作,如果没有,makedispatch: true
将创建一个无限循环。我认为仅使用tap
不会破坏效果的流动。this._alertService.update
使用和我的SAVE_UPDATE_ONE_SUCCESS
效果时也会发生同样的事情。我使用这些订阅来确认操作成功完成,关闭加载指示器,然后包含在其他 Observable 管道链中,所以我有点需要它。
如何让添加操作的 Observable 再次发出?谢谢!!
this._alertService.add({ title: 'test', isEnabled: true }).subscribe({
next: (val) => console.log(`next called: ${val}`), // never will fire
error: (err) => console.error(`error called: ${err}`), // never will fire
complete: () => console.log(`complete called`), // never will fire
});
这是效果:
@Injectable()
export class AlertEffects {
/** Effect to watch for any time a new `Alert` is added. */
ngrxDataEffectForAlertAdded$ = createEffect(
() =>
this.actions$.pipe(
ofEntityType('Alert'),
ofEntityOp(
EntityOp.SAVE_ADD_ONE_SUCCESS,
EntityOp.SAVE_ADD_MANY_SUCCESS
),
tap((action) => {
const alert = action.payload.data as Alert;
if (alert.isEnabled) {
this._alertService.update(
{ ...alert, localNotificationId: null, isEnabled: false },
{
correlationId: 'do-not-trigger-update-local-notification-effect',
tag: 'Disable New Alert',
}
);
}
})
),
{ dispatch: false }
);
private readonly _alertService: EntityCollectionService<Alert>;
constructor(private actions$: Actions, appEntityServices: AppEntityServices) {
this._alertService = appEntityServices.alertService;
}
}