首页 文章

ngrx效果:派遣空行动

提问于
浏览
0

如何让我的ngrx / store效果发送空动作?我正在运行Angular 6 / rxjs 6:

@Effect()
  testEffect$ = this.actions$.ofType(FIRST_ACTION).pipe(
    map((x) => {
       if (x === y) {
          return new SECOND_ACTION;
       } else {
          fire.some.effect;
          return EMPTY;
       }
    })
  );

目前,我遇到两个错误: Effect "testEffect$" dispatched an invalid action: [object Object] 后跟 TypeError: Actions must have a type property .

我发现this answer,但它没有尝试以下(没有工作):

EMPTYObservable.empty()Observable.of()Observable.of([]){ type: 'EMPTY_ACTION' }

任何帮助,将不胜感激!我知道我可以使用 { dispatch: false } ,但实际效果大约有五个结果,其中只有一个没有't use an action, so I' d而是最后一个也返回了一些东西 .

3 回答

  • -1

    你可以使用过滤器

    @Effect()
    testEffect$ = this.actions$.ofType(FIRST_ACTION).pipe(
      filter(x => x === y),
      map( x => new SECOND_ACTION)
    )
    

    如果你仍然需要另一个案例你可以用 dispatch: false 写另一个效果

  • 0

    这对我有用(ng6):

    @Effect()
    boardOpened$ = this.actions$
      .ofType<BoardActions.Open>(BoardActions.OPEN)
      .pipe(
        withLatestFrom(this.store.select(BoardSelectors.getState)),
        map(([action, state]: [Action, BoardReducer.State]) => {
          return !BoardReducer.isLoaded(state)
            ? new BoardActions.Load()
            : EMPTY;
        })
      );
    

    要么

    @Effect()
    boardOpened$ = this.actions$
      .ofType<BoardActions.Open>(BoardActions.OPEN)
      .pipe(
        withLatestFrom(this.store.select(BoardSelectors.getState)),
        switchMap(([action, state]: [Action, BoardReducer.State]) => {
          return !BoardReducer.isLoaded(state)
            ? of(new BoardActions.Load())
            : EMPTY;
        })
      );
    
  • 2

    这是一个可能的解决方案:

    @Effect()
      testEffect$ = this.actions$.ofType(FIRST_ACTION).pipe(
        tap((x) => { // do some side effect here
            if (x !== y ) {
                fire.some.effect;
            }
        }),
        filter((x) => x === y), // proceed only if condition is true
        map((x) => {
           return new SECOND_ACTION; // then return the needed action
        })
      );
    

相关问题