【資料圖】
@toc
在/utils新建request
import axios from "axios"import bus from "@/utils/bus"import {Message} from "element-ui"const service = axios.create({ baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url // baseURL: "/api", // url = base url + request url timeout: 100000, // request timeout})service.interceptors.request.use( config => { let currentUser = JSON.parse(sessionStorage.getItem("UserInfo")) if (currentUser && currentUser.token) { config.headers["Authorization"] = "token " + currentUser.token } return config }, error => { console.log(error) return Promise.reject(error) })// response interceptorservice.interceptors.response.use( response => { return response }, error => { const res = error.response.data const status = error.response.status if (status >= 500) { Message.error("服務(wù)繁忙請(qǐng)稍后再試") } else if (status >= 400) { if (res.status === 401) { Message.warning("您的賬號(hào)登錄已失效, 請(qǐng)重新登錄") bus.$emit("logout") } else { Message.error("未知錯(cuò)誤") console.error(error) } } return Promise.resolve(error) })export default service
接口js頁(yè)面中
// 卡片管理模塊import request from "@/utils/request"http:// getexport function getList(params) { return request({ url: "/", method: "get", params })}// POSTexport function postList(data) { return request({ url: "/", method: "POST", responseType: "arraybuffer", data })}
vue頁(yè)面
//引入import { getList, postList} from "@/api/comparison.js";//使用 getList().then((res) => { });
標(biāo)簽: