Encapsulate Axios in Vue
In the previous project, the request API and the request method are encapsulated. This encapsulation is for simplicity, better management of the interface given by the backend, reusability of the request code, and simplification of the code. Install axios $ npm install axios Create directory File Create http directory in src Create http.js user in http directory so request method Create in http directory api.js is used to store the backend interface create axios.js user in the http directory as an axios interceptor create vue.config.js user request proxy under the root directory Configuration Next is the code The code in the project/scr/http/http.js import axios from & # 39; axios & # 39;; export default { /** * get request * @param url interface route * @param auth Whether to bring login information * @returns {AxiosPromise} */ get(url, auth = false) { if (auth) { return axios.get(url, {headers: {Authorization: 'Your back-end user authenticates information'}}); } else { return axios. get(url); } }, /** * post request * * @param url interface route * @param data interface parameters * @param auth Whether to bring login information * @returns {AxiosPromise} */ post(url, data, auth = false) { if (auth) { return axios.post(url, data,…