ant-design/ant-design-pro

🧐[问题 | question]如何添加token

mayongkang1 opened this issue · 2 comments

由于这个代码会在全局加载一次,这时还没有登录,所以console.log('执行了', token);,的token为null
导致后续的请求添加token时获取到的都是空,如何在登录之后手动再加载一次配置,求解

import type { RequestOptions } from '@@/plugin-request/request';
import {message} from "antd";

const token = JSON.parse(localStorage.getItem('token'))
console.log('执行了', token);
/**

  • 错误处理
    */
    export const errorConfig = {
    // 添加token
    headers: {
    'Authorization': ${token.tokenType} ${token.accessToken},
    },

// 请求拦截器
requestInterceptors: [
(config: RequestOptions) => {
// 拦截请求配置,进行个性化处理。
const url = config?.url
return { ...config, url };
},
],

// 响应拦截器
responseInterceptors: [
(response) => {
// 拦截响应数据,进行个性化处理
const { data } = response as unknown as API.Response;

  if (data?.success === false) {
    message.error('请求失败!');
  }
  return response;
},

],
};

当前 Issue 未检测到标题,请规范填写,谢谢!

The title of the current issue is not detected, please fill in according to the specifications, thank you!