Simple encapsulation of Axios

download

npm install axios -S
Copy the code

Introduced the global

Register globally at main.js

import axios from 'axios'
Vue.prototype.$axios = axios
Copy the code

Create the AXIos folder

Create the API folder in SRC. Create the axios folder. Inside the axios folder, create another server.js file and request.js fileCopy the code

1. Create an axios instance in the server.js file and request interception

Import axios from 'axios / / create instances const axios service = axios. Create ({}) / / t add interceptors service. The interceptors. Request. Use (config = > {the console. The log (' is requesting) return the config}) / / add the response to intercept service. The interceptors. Response. Use (response = > {the console. The log (' request is successful)  return response.data }, error => { console.log('TLC:error', error) return Promise.reject(error) }) export default service;Copy the code

2. Encapsulate the obtained data in Request. js

import axios from "./Server" export class firstApi { static getFisrtList() { return Axios. Get (` ` http://192.168.0.103:8081/index. Then (res = > res)}}Copy the code