NgRx/Store 使用示例

创建已下文件:

index.ts

export * from './video-gis.selector';
export * from './video-gis.actions';
export * from './video-gis.reducer';
export * from './video-gis.module';

video-gis.actions.ts

import { Action } from '@ngrx/store';
import {VideoInfo } from './video-gis.reducer';

export enum ActionTypes {
  SetVideoInfo = '[Video GIS] Set Video Info', // 编辑视频信息
  AddVideoInfo = '[Video GIS] Add Video Info', // 添加视频信息
  UpdateVideoInfo = '[Video GIS] Update Video Info', // 更新视频信息
  DeleteVideoInfo = '[Video GIS]  Delete Video Info', // 删除视频信息
}

export class SetVideoInfo implements Action {
  readonly type = ActionTypes.SetVideoInfo;

  constructor(public payload: VideoInfo[]) { }
}

export class AddVideoInfo implements Action {
  readonly type = ActionTypes.AddVideoInfo;

  constructor(public payload: VideoInfo) { }
}

export class UpdateVideoInfo implements Action {
  readonly type = ActionTypes.UpdateVideoInfo;

  constructor(public payload: VideoInfo) { }
}

export class DeleteVideoInfo implements Action {
  readonly type = ActionTypes.DeleteVideoInfo;

  constructor(public index: number) { }
}

export type ActionUnion =
  SetVideoInfo |
  AddVideoInfo |
  DeleteVideoInfo |
  UpdateVideoInfo ;

video-gis.module.ts

import { NgModule } from '@angular/core';
import { StoreModule } from '@ngrx/store';

import {VideoInfoReducer } from './video-gis.reducer';

@NgModule({
  imports: [
    StoreModule.forFeature('videoInfo', VideoInfoReducer)
  ]
})
export class VideoGISModule { }

video-gis.reducer.ts

import { AppState } from '../app.state';
import * as Actions from './video-gis.actions';

export interface VideoInfo {
  id: string;
  name: string;
  show: boolean;
  data: any;
}

export interface State extends AppState {
  videoInfo: VideoInfo[];
}

export const initialVideoInfoState: VideoInfo[] = videoInfoListData; // 默认值

export function VideoInfoReducer(
  state = initialVideoInfoState,
  action: Actions.ActionUnion
): VideoInfo[] {
  console.log(action.type);
  switch (action.type) {
    case Actions.ActionTypes.SetVideoInfo:
      return action.payload;

    case Actions.ActionTypes.AddVideoInfo: {
      const _state = state ? [...state] : [];
      _state.push(action.payload as VideoInfo);
      return _state;
    }

    case Actions.ActionTypes.DeleteVideoInfo: {
      const _state = [...state];
      _state.splice(action.index, 1);
      return _state;
    }

    case Actions.ActionTypes.UpdateVideoInfo: {
      const _state = [...state];
      const idx = state.findIndex(path => path.name === action.payload.name);
      _state[idx] = action.payload;
      return _state;
    }

    default:
      return state;
  }
}

video-gis.selector.ts

import { createFeatureSelector } from '@ngrx/store';
import { State, VideoInfo } from './video-gis.reducer';

export const selectVideoInfo = createFeatureSelector<State, VideoInfo[]>('videoInfo');

使用:
在对应的componenet中:

public videoInfos: VideoInfo[];
constructor(private store: Store<AppState>) {
    // 获取视频列表
    this.store.pipe(select(selectVideoInfo)).subscribe(data => {
      this.videoInfos = data || [];
    });
 }
 // 使用方法
 saveVideoInfoState(){
    this.store.dispatch(new SetVideoInfo(this.videoInfos));
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值