fetch封装 dva中调用

//utils文件夹的indexjs文件
const request = (url, option = {}) => {
    if (option.body) {
        option.body = JSON.stringify(option.body)
    }
 let newOption = {
        headers: {
            'Content-Type': 'application/json',
            "Authorization": localStorage.getItem('token')
        },
        ...option
    };
    return fetch(url, newOption).then(res => res.json()).catch((err) => alert(err));
}
export default request;

//service文件夹index.js文件
export function UserApi_authority() {//get请求接口
    return request('/user/api_authority');
}
export function DeleteApiInterFace(body) {//post请求接口
    return request('/user/delAutho/delApi', {
        method: "post",
        body
    });
}

//react中model文件夹中调用
import {UserApi_authority,DeleteApiInterFace} from '../service'
export default {
    namespace: 'exam',
    state:{
    },
     reducers: {
        changeState(state, action) {
            return {...state, ...action }
        }
    },
    effects: {
          * UserApi_authority(action, { call, put }) {
            let result = yield call(UserApi_authority);
            yield put({ type: 'changeState', UserviewAuthority: result.data });
        },
        * DeleteApiInterFace(action, { call, put }) {
            let result = yield call(DeleteApiInterFace, action.action);
            if (result.code == 1) {
                let result = yield call(UserApi_authority);
                yield put({ type: 'changeState', UserviewAuthority: result.data });
            }
        },
     }
}

//在需要的页面中dispatch发起
import {connect} from "dva"
class Login extends Component {
    componentDidMount(){
        this.props.dispatch({
             type:'exam/UserApi_authority',
             payload//如果需要参数
        });
    }
    render() {
        const { getFieldDecorator } = this.props.form;
        return (
            <div></div>
        );
    }
}
let mapState= store=>{
    return {...store.exam}
}
export default connect(mapState)(Form.create()(Login));
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。