You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
canteen/request.js

20 lines
503 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import axios from "axios";
const req = axios.create({
baseURL: "http://localhost:3000",
timeout: 2000
})
// 添加请求拦截器
req.interceptors.request.use(function (config) {
// 在发送请求之前做些什么
if (localStorage.getItem("token")) {
//有token则默认发送给服务器
config.headers.Authorization = `Bearer ${localStorage.getItem("token")}`
}
return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
});
export default req