1.适当封装 Redux,简化调用
src/utils/redux.js
/** * 适当封装 Redux,简化调用 */ /* eslint-disable import/prefer-default-export */ import fetch from './request' export function createAction(options) { const { url, payload, method, fetchOptions, cb, type } = options return (dispatch) => { return fetch({ url, payload, method, ...fetchOptions }).then((res) => { dispatch({ type, payload: cb ? cb(res) : res }) return res }) } }
2.调用
src/actions/user.js
import { API_USER_LOGIN } from '@constants/api' import { createAction } from '@utils/redux' /** * 用户登录 * @param {*} payload */ export const dispatchLogin = payload => { return createAction({ url: API_USER_LOGIN, // api请求地址 type: USER_LOGIN, payload }) }
.