model( redux )文件中

state

  • 定义state
  1. // XxxModel 为 xxxModel 的类型
  2. export interface xxxSate{
  3. 数据名1: 类型1,
  4. 数据名2: 类型2
  5. }
  6. interface XxxModel {
  7. namespace: string;
  8. state : xxxSate
  9. }
  10. const xxxModel : XxxModel = {
  11. namespace: 'xxxModel',
  12. state: {
  13. 数据名1: 初始数据1,
  14. 数据名2: 初始数据2
  15. }
  16. }
  • effects( e发可斯 )
  1. import { Effect } from 'umi';
  2. interface XxxModel {
  3. namespace: string;
  4. state : xxxSate;
  5. effects: {
  6. getXxx: Effect
  7. }
  8. }
  9. const xxxModel : XxxModel = {
  10. namespace: 'xxxModel',
  11. effects: {
  12. *getXxx ( { payload }, { put, call } ) {
  13. ...
  14. }
  15. }
  16. }
  • reducers( 里丢色斯 )
  1. import { Effect , ImmerReducer } from 'umi';
  2. import * as services from '@/services';
  3. interface XxxModel {
  4. namespace: string;
  5. state : xxxSate;
  6. effects: {
  7. getXxx: Effect
  8. };
  9. reducers: {
  10. GET_XXX: ImmerReducer
  11. }
  12. }
  13. const xxxModel : XxxModel = {
  14. namespace: 'xxxModel',
  15. effects: {
  16. *getXxx ( { 页面传递的参数 }, { put, call } ) {
  17. const 请求到的数据 = yield call( services.请求的方法 , 页面传递的参数 )
  18. yield put({
  19. type: "GET_XXX",
  20. payload: 请求到的数据
  21. })
  22. }
  23. }
  24. reducers: {
  25. GET_XXX( state , { payload}){
  26. 改变state...
  27. }
  28. }
  29. }