【问题标题】:ngrx effect's action not triggering reducerngrx 效果的动作未触发减速器
【发布时间】:2018-02-27 03:56:23
【问题描述】:

我的 Angular 5 代码在没有 ngrx 效果的情况下工作。添加效果并发送HTTP请求后,我无法触发reducer被动作调用。我的效果是这样的。此控制台日志受到正确的 http 响应的影响。

@Injectable()
export class PropertyEffects {
@Effect({dispatch: false})
propertyFetch$: Observable<Action> = this.actions$//.pipe(
    .ofType(PropertyActions.FETCH_ALL_PROPERTIES)
    .switchMap(action =>
       this.httpClient.get('https://yxalbf1t6l.execute-api.us-east-1.amazonaws.com/dev/todos').pipe(
            // If successful, dispatch success action with result
            map(data => {
                console.log(`Success ${JSON.stringify(data)}, 0, 2)`);
                return new PropertyActions.OpenAllProperties(data)
            })
    )
); 

下面的 OpenAllProperties 操作的减速器在使用效果之前被正确调用。实际上,它的控制台日志永远不会被命中。

case PropertyListActions.OPEN_ALL_PROPERTIES:
   console.log("in reducer, action.payload: " + JSON.stringify(action.payload,null,2))
        const openAllProperties = !state.openAllProperties;
        return {
            ...state,
            openAllProperties: openAllProperties
        }

请帮忙。谢谢!

【问题讨论】:

  • 我认为{dispatch: false} 参数正在阻止传递操作。

标签: angular ngrx ngrx-effects


【解决方案1】:

您正在传递一个参数来停止调度事件。这不会触发 reducer 函数,因为它不分派任何东西。

如果您想验证它,请使用redux dev tools

在那里您将看到应用程序中的所有调度事件。

【讨论】:

    【解决方案2】:
    performSearchAction$: Observable<Action> = createEffect(
    () =>
      this.actions$.pipe(
        ofType(actions.searchAction),
    
        switchMap(data =>
          this.apiService.getSearch(data).pipe(
            tap(x => console.log('searchResponseData', x)),
            map(searchResponseData => actions.searchSuccessAction(searchResponseData)),
            catchError(error => of(actions.searchFailureAction(error)))
          )
        )
      ),
    { dispatch: true, resubscribeOnError: false } // dispatch: true
    

    );

    提示:记得设置:dispatch: true 如果使用ngrx8

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      相关资源
      最近更新 更多