1,使用 umi-request 替换 umi 的配置。
安装umi-request
npm i umi-request
//import { request } from 'umi'; // -- 默认请求无法进行header token 认证
import request from 'umi-request'; // -- 新版设置request请求拦截// request拦截器, 改变url 或 options.
request.interceptors.request.use((url, options) => {
let token = localStorage.getItem('token');if (null === token) {
token = '';}const authHeader = {
Authorization: `Bearer ${
token}` };return {
url: url,options: {
...options, interceptors: true, headers: authHeader },};
});
2,前端完整代码
//import {request} from 'umi'
import request from 'umi-request';// request拦截器, 改变url 或 options.
request.interceptors.request.use((url, options) => {
let token = localStorage.getItem('token');if (null === token) {
token = '';}const authHeader = {
Authorization: `${
token}` };return {
url: url,options: {
...options, interceptors: true, headers: authHeader },};
});export default request;