目录
- umi+react+dva 配置request的全局配置(请求拦截与响应拦截)和调用请求
-
- 配置request的全局配置(请求拦截与响应拦截)
-
- 使用useRequest或者普通方法 调用请求
-
umi+react+dva 配置request的全局配置(请求拦截与响应拦截)和调用请求
配置request的全局配置(请求拦截与响应拦截)
app.js
export const request = {
timeout: 1000 * 10,
requestInterceptors:[
(url,options) => {
console.log('请求拦截',url,"options",options);
return options
}
],
responseInterceptors:[
(response,options) => {
console.log('响应拦截',response,"options",options);
return response
}
]
}
使用useRequest或者普通方法 调用请求
about.jsx
import React, { useState, useEffect } from 'react';
import { Space, Table, Button ,message} from 'antd';
import { getStyList, delStyList } from '@/api/about.js';
import { useRequest } from "umi"
export default function About_1(props) {
const columns = [
{
title: '姓名',
dataIndex: 'name',
key: 'name',
},
{
title: '日期',
dataIndex: 'date',
key: 'date',
},
{
title: '分数',
dataIndex: 'score',
key: 'score',
},
{
title: '住址',
dataIndex: 'city',
key: 'city',
},
{
title: 'Action',
dataIndex: '',
key: 'action',
render: (text, record) => (
<Space size="middle">
<Button type="primary" size="small" onClick={() => edit(record)}>
编辑
</Button>
<Button
type="primary"
danger
size="small"
onClick={() => del(record)}
>
删除
</Button>
</Space>
),
},
];
useEffect(() => {
}, []);
const init = async () => {
setLoading(true)
const res = await getStyList();
console.log('res', res);
setLoading(false)
if (res.code == 200) {
setData(res.data);
} else {
message.warning(res.msg);
}
};
let { data = [], loading, error,run: getStyListReq } = useRequest(async () => {
let res = await getStyList()
console.log('res',res);
return {
data: res.data ? res.data : []
}
})
console.log('data, loading, error',data, loading, error,getStyListReq);
const rowKeyFn = (row) => row.id;
const edit = (row) => {
console.log('edit', row);
};
const del = async (row) => {
const res = await delStyList(row.id);
if ( res.code ) {
message.success("删除成功");
getStyListReq();
} else {
message.warning(res.msg);
}
};
return (
<div className="content">
<Table
dataSource={data}
columns={columns}
rowKey={rowKeyFn}
size="middle"
loading={loading}
/>
</div>
);
}