在src目录下面创建一个api文件夹 api里面创建index.js和request.js文件
index.js
import axios from "axios"var service = axios.create({ baseURL: "/api", //所有的请求都会带上 /api "content-type": "application/json", timeout: 5000})//请求拦截器// service.interceptors.request.use((config)=>{// console.log("发请求了 以后要在这里带上令牌")// return config// })// 响应拦截器// service.interceptors.response.use((rse) => {// return rse;// })export default service;
request.js
import axios from "./index" // 请求export const getList = () => { return axios.get("path") //跨域要配置代理}
组件如果想要调用,只需要引用 request.js 调用方法就好了。