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,…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索