1、导入axios
npm i axios
2、创建request.js 文件
process.env.VUE_APP_BASE_URL会根据开发还是线上获取动态接口
import axios from "axios";
import { getToken } from "./auth"
const instance = axios.create({
baseURL: process.env.VUE_APP_BASE_URL,
headers: {
'Content-Type': 'application/json;charset=utf-8'
}
})
instance.interceptors.request.use(
(config) => {
if (getToken()) {
config.headers["Authorization"] = 'Bearer ' + getToken()
}
return config
},
(error) => {
return Promise.reject(error)
}
)
export default instance
3、添加两个环境文件(无测试)