2018-09-04 DVA ---model中多个reducer

DVA中的model:

import {
  alibabaIdCardIdentify,
  alibabaVehicleLicenceIdentify,
  alibabBusinessLicenceIdentify,
  getSTSToken,
} from '../services/alibabaService';

export default {
  namespace: 'alibaba',

  state: {
    alibabaData: {},
    alibabaAuth: {},
    alibabaBusinessLicence:{}
  },

  effects: {
    // 身份证
    *alibabaIdCardIdentify({ payload }, { call, put }) {
      const response = yield call(alibabaIdCardIdentify, payload);
      yield put({
        type: 'alibabaData',
        payload: response,
      });
    },
    // 驾驶证
    *alibabaVehicleLicenceIdentify({ payload }, { call, put }) {
      const response = yield call(alibabaVehicleLicenceIdentify, payload);
      yield put({
        type: 'alibabaData',
        payload: response,
      });
    },
    // 营业执照
    *alibabBusinessLicenceIdentify({ payload }, { call, put }) {
      const response = yield call(alibabBusinessLicenceIdentify, payload);
      yield put({
        type: 'alibabaBusinessLicence',
        payload: response,
      });
    },
    // 获取阿里巴巴STS授权
    *getSTSToken({ payload }, { call, put }) {
      const response = yield call(getSTSToken, payload);
      yield put({
        type: 'alibabaAuth',
        payload: response,
      });
    },
  },

  reducers: {
    alibabaData(state, action) {
      return {
        ...state,
        alibabaData: action.payload,
      };
    },
    alibabaAuth(state, action) {
      return {
        ...state,
        alibabaAuth: action.payload,
      };
    },
    alibabaBusinessLicence(state, action) {
      return {
        ...state,
        alibabaBusinessLicence: action.payload,
      };
    },
  },
};

reducer中的alibabaBusinessLicence对应state中的alibabaBusinessLicence

return {
      ...state,
        alibabaBusinessLicence: action.payload,
      };
state: {
    alibabaData: {},
    alibabaAuth: {},
    alibabaBusinessLicence:{}
  },

reducer的名字对应effects中的type

 yield put({
        type: 'alibabaBusinessLicence',
        payload: response,
      });

最后要在模板中访问alibabaBusinessLicence这个state的值,只需要根据当前model的name(alibab)来获取

  this.props.alibaba.alibabaBusinessLicence

你可能感兴趣的:(2018-09-04 DVA ---model中多个reducer)