ngrx 效果的动作不触发减速器
ngrx effect's action not triggering reducer
我的 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
}
请帮忙。谢谢!
您正在传递一个参数来停止调度事件。这不会触发 reducer 函数,因为它不派发任何东西。
如果你想验证它使用redux dev tools
在那里你会看到应用程序中的所有调度事件。
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
我的 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
}
请帮忙。谢谢!
您正在传递一个参数来停止调度事件。这不会触发 reducer 函数,因为它不派发任何东西。
如果你想验证它使用redux dev tools
在那里你会看到应用程序中的所有调度事件。
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
);