通过组件输出在ngrx存储外部触发副作用

问题描述 投票:0回答:1

我有一个带有模态的模块,在其中执行一些表单工作,模态具有很小的特征存储。

[完成工作(触发:成功保存)后,我需要触发输出事件,以便我的上下文(包装我的模态的组件)可以关闭模​​态。我无法通过经典操作-上下文不知道我的操作,只能响应组件上的输出。

所以我现在拥有的是这个:

@Component MyComponent {
  @Output() closeModal = new EventEmitter();
  constructor(private store: Store<MyState>, private actions: Actions) {
    // Here I subscribe to actions. Is there another way to do this?
    this.actions.pipe(
      ofType(actionTypes.SaveSuccess),
      tap(() => this.closeModal.emit(),
    );
  }
}

有更好的方法吗?将动作流导入组件似乎是错误的,为此,我想避免不得不向状态界面添加新键。

编辑:为了澄清,我想基于一些ngrx动作在我的组件上触发一个Output事件。

angular ngrx ngrx-effects side-effects
1个回答
0
投票

我根据服务将效果放在单独的文件中

假设我们有一个http.service.ts,我将制作一个http-efects.service.ts文件以使组件尽可能简单。

Http-effects.service.ts

import {HttpService} from '../services/http.service';
import {
MyAction,
MY_ACTION,
} from '../actions.';

@Effect() myAction$: Observable<Action> = this.actions$.pipe(
        ofType<MyAction>(MY_ACTION),
    switchMap((action) => {
        return this.httpService.postAction(action.payload)
            .pipe(
                map((data) => new saveSuccess(data)),
                catchError(data => of(new saveError(data.error.message)))
            );
    }));
© www.soinside.com 2019 - 2024. All rights reserved.