2025-02-23 2025-02-23 Vue 官方文档 Axios 操作步骤 cd到项目根目录下,安装Axios包。 [npm]1npm install axios [yarn]1yarn add axios 在项目根目录下创建api目录(名字请随意),并在其中创建index.js文件,用于进行二次封装。 在index.js文件中,引入Axios包,并进行封装,如下所示 12345678910111213141516171819202122232425262728293031323334353637383940import axios from "axios";import router from "@/router/index";// 二次封装axios;const service = axios.create({ baseURL: "http://localhost:9099/api/v1", // 基础 URL timeout: 5000, // 请求超时时间});// const service = axios.create({// baseURL: "/api/v50", // 基础 URL// timeout: 5000, // 请求超时时间// });// 添加响应拦截器service.interceptors.response.use(function (response) { // 2xx 范围内的状态码都会触发该函数。 // 对响应数据做点什么 return response;}, function (error) { console.log(error); // 超出 2xx 范围的状态码都会触发该函数。 // 对响应错误做点什么 // if (error.response.status === 401) { // // 401 未授权 // router.push('/'); // message.error(error.response.data.msg); // } return Promise.reject(error);});export default service; 前一篇 Vue - 关联数组的声明和使用 后一篇 Vue - Vscode中为Vue配置代码片段