我的项目中有一个错误,我找不到问题所在。 我在NGRX开发人员工具中看到触发了该操作,并使用新数据使状态变新,但是我在控制台中收到此错误,这使我感到困扰。
这是我的状态:
export interface User {
userId: number;
id: number;
title: string;
completed: boolean;
}
动作:
import { createAction, props } from '@ngrx/store';
import { User } from '../models/user.model';
export const loaduser = createAction(
'[USER] Load',
props<User>()
);
减速器:
import { User } from '../models/user.model';
import * as UserAction from '../actions/user.actions';
import { Action, createReducer, on } from '@ngrx/store';
export const initialState: User = null;
const commentReducer = createReducer(
initialState,
on(UserAction.loaduser, (state, payload) => payload)
);
export function reducer(state: User, action: Action) {
return commentReducer(state, action);
}
带有api的组件:
ngOnInit() {
this.userService.getUsers().subscribe((data) => {
this.store.dispatch(loaduser(data));
});
}
答案 0 :(得分:0)
您的userService
getUsers方法可能未设置正确的返回类型(应为Observable<User>
),因此预订假定响应为未知对象,然后尝试将其作为类型的参数传递User
。
这应该解决它:
ngOnInit() {
this.userService.getUsers().subscribe((data: User) => {
this.store.dispatch(loaduser(data));
});
}
请注意data
参数上的类型。但是,您还应该确保正确键入服务。