根据umi-request文档做了一个极简单的封装,错误处理,中间件可以根据文档自行添加
请求代理在.中配置proxy
import { extend } from "umi-request";
const errmsgMap = {
}
const HttpRequest = function ({url, method, params}) {
let config = {
method,
params: (method == 'GET' || method == 'get') ? params : {},
data: (method == 'POST' || method == 'post') ? params : '',
timeout: 5000,
headers: {
'Content-Type': 'multipart/form-data',
},
prefix: '',
suffix: '',
errorHandler: function (error) {
console.log('异常:', error);
if (error.response) {
}
throw error.response
}
}
console.log(url + '请求参数:', config);
const request = extend(config)
return request(url)
}
export default HttpRequest;
import HttpRequest from './request'
export default {
testApi: () => HttpRequest({
url: '/api/v2/movie/in_theaters',
method: 'get',
params: {
apikey: '0b2bdeda43b5688921839c8ecb20399b'
}
})
}
import { useEffect } from "react";
import api from "../../../api/api";
export default function CurrencyListPage() {
useEffect(() => {
api.testApi().then(res => {
console.log(res);
})
})
return (
<div>列表</div>
)
}