umi-request
antd pro 拉下來的request,用的是
import { extend } from 'umi-request';
const request = extend({
....
headers: {
....
token:localStorage.getItem("token")
},
});
想要每次請求帶上token家破,就在headers里設(shè)置 token:localStorage.getItem("token")
但是有時(shí)候會(huì)發(fā)現(xiàn)token 明明獲取到了赐稽,但是headers上寫不進(jìn)去
原因
extend只會(huì)在初始化的時(shí)候設(shè)置一遍翰萨,之后用的都是初始化時(shí)候配置的值
解決:
request.interceptors.request.use(async (url, options) => {
if (
options.method === 'post' ||
options.method === 'put' ||
options.method === 'delete' ||
options.method === 'get'
) {
const headers = {
'Content-Type': 'application/json',
Accept: 'application/json',
token:localStorage.getItem("token")
};
return {
url,
options: { ...options, headers },
};
}
});
每次請求都在發(fā)出前缺狠,手動(dòng)攔截一下尖淘,手動(dòng)加上token