Angular NGRX错误“;Dispatch需要一个对象,但收到了一个函数。如果你';重新使用createAction函数…”;
我遵循Youtube上的一个教程,尝试创建自己的项目,但我在浏览器中不断收到一个错误,说Dispatch需要一个对象 我的模型:Angular NGRX错误“;Dispatch需要一个对象,但收到了一个函数。如果你';重新使用createAction函数…”;,angular,reactive-programming,ngrx,Angular,Reactive Programming,Ngrx,我遵循Youtube上的一个教程,尝试创建自己的项目,但我在浏览器中不断收到一个错误,说Dispatch需要一个对象 我的模型: export interface Football { title: string; embed: string; date: string; url: string; thumbnail: string; } export interface State { footballs: Football[]; isLoading: boolea
export interface Football {
title: string;
embed: string;
date: string;
url: string;
thumbnail: string;
}
export interface State {
footballs: Football[];
isLoading: boolean;
error: string;
}
export const initialState: State = {
footballs: null,
isLoading: false,
error: '',
};
行动:
export const loadAllSucceeded = createAction('[Football Api] Load All succeeded', props<{footballs: Football[]}>());
export const loadAllFailed = createAction('[Football Api] Load All succeeded', props<{error: string}>());
export const appComponentInitialized = createAction('[App Component] Initialized');
export const loadAllRequested = createAction('[App Component] Load All Requested');
影响:
@Injectable()
export class FootballStoreEffects {
constructor(private footballService: FootballService, private actions$: Actions) {
}
loadAll = createEffect(() =>
this.actions$.pipe(
ofType(FootballActions.loadAllRequested),
switchMap(() =>
this.footballService.getFootballVideos()
.pipe(
map(footballs => FootballActions.loadAllSucceeded({footballs})),
catchError(error => of(FootballActions.loadAllFailed({error})))
)))
);
}
减速器:
const featureReducer = createReducer(
initialState, on(FootballActions.loadAllRequested, state => ({...state, isLoading: true, error: ''})),
on(FootballActions.loadAllSucceeded, (state, props) => ({... state, isLoading: false, error: '', footballs: props.footballs})),
on(FootballActions.loadAllFailed, (state, {error}) => ({... state, isLoading: false, error}) )
);
export function reducer(state: State | undefined, action: Action) {
return featureReducer(state, action);
}
服务方法:
getFootballVideos(): Observable<Football[]> {
return this.http.get<FootballResult>(this.API_BASE_URL).pipe(map(result => result.value));
}
getFootballVideos():可观察{
返回this.http.get(this.API_BASE_URL).pipe(map(result=>result.value));
}
组成部分:
export class FootballVideosComponent implements OnInit {
footballs$: Observable<Football[]>;
error$: Observable<string>;
isLoading$: Observable<boolean>;
constructor(private store: Store<State>) { }
ngOnInit() {
this.footballs$ = this.store.pipe(select(FootballSelectors.selectFootballList));
this.error$ = this.store.pipe(select(FootballSelectors.selectFootballError));
this.isLoading$ = this.store.pipe(select(FootballSelectors.selectFootballIsLoading));
}
onRefresh() {
this.store.dispatch(FootballActions.loadAllRequested);
}
}
导出类足球视频组件在NIT上实现{
足球$:可观察的;
误差$:可观测;
isLoading$:可观察到;
构造函数(私有存储:存储){}
恩戈尼尼特(){
this.footballs$=this.store.pipe(选择(FootballSelectors.selectfootballslist));
this.error$=this.store.pipe(select(FootballSelectors.selectFootballError));
this.isLoading$=this.store.pipe(选择(FootballSelectors.selectfootballsloading));
}
onRefresh(){
此.存储.发送(要求进行鞋垫安装.装载);
}
}
执行onRefresh()时,我在浏览器中遇到此错误:
错误类型错误:
Dispatch需要一个对象,但收到了一个函数。
如果您正在使用createAction函数,请确保调用该函数
在发送操作之前。例如,someAction应该是someAction()。
在actionsObject.next(store.js:159)
在Store.dispatch(Store.js:704)
在FootballVideosComponent.onRefresh(football videos.component.ts:29)
at Object.eval[as handleEvent](football videoscomponent.html:3)
在handleEvent(core.js:43993)
在callWithDebugContext(core.js:45632)
在Object.debugHandleEvent[作为handleEvent](core.js:45247)
在dispatchEvent(core.js:29804)
在core.js:42925
在HTMLButtoneElement。(platform browser.js:2668)尝试添加()
,如下所示:
onRefresh() {
this.store.dispatch(FootballActions.loadAllRequested());
}
onRefresh() {
this.store.dispatch(FootballActions.loadAllRequested());
}