1

我正在尝试使用没有类似模型的 ngrx 存储状态

export interface AppStore{
    competitions:any[];
}

但是当它返回未定义时,我现在不知道如何在任何我厌倦了搜索它时使用动作有效负载,请帮助

this.store.select('competitions').subscribe(data=> console.log(data)).unsubscribe();

我总是不确定请帮助

使用 NGRX

在组件中创建了一个状态

export interface AppState{
    comp:any;
}

ngOnInit(){
        this.store.dispatch({type:GET_COMP,payload: {}});
}

行动

export const GET_COMP = 'GET_COMP';
export const SUCCESS = 'SUCCESS';

export class GetAction implements Action {
  readonly type = GET_COMP;

  constructor(public payload: any) {}
}

export class SuccessAction implements Action {
  readonly type = SUCCESS;

  constructor(public payload: any) {}
}

export type Actions =
  | GetAction
  | SuccessAction
;

效果

@Injectable()
export class MainEffects {

  constructor(private action$: Actions, private service$ :CompetitionService ) { }

  @Effect() load$:Observable<Action> = this.action$
      // Listen for the 'LOGIN' action
      .ofType(GET_COMP)
      .switchMap(action => this.service$.getComp()
        // If successful, dispatch success action with result
        .map(res => ({ type: SUCCESS, payload: res}))
        // If request fails, dispatch failed action
        .catch((err) => Observable.of({ type: 'FAILED' ,payload :err}))  
      );
}

减速器

export function MainReducer(state = [],action: GetAction) {
  switch (action.type) {
    case GET_COMP:
            return [action.payload];
        default:
            return state;
  }
}

应用程序模块

StoreModule.forRoot({MainReducer}),
EffectsModule.forRoot([MainEffects])

减速机厂

export const reducers: ActionReducerMap<AppStore> = {
     competitions: MainReducer
};
4

0 回答 0