In Vue, we could not intentionally introduce jquery for Web requests, before the Vue had vue-resource to solve the network request problem, and then the official suggested using Axios, while Vue-resource was no longer maintained.
For a detailed tutorial on Axios, see GitHub Address:
Https://github.com/axios/axios
Installation:
NPM Install Axios
Or
<script src= "Https://unpkg.com/axios/dist/axios.min.js" ></script>
Use:
First, the configuration file type:
Axios (config). Then (res=>{to-do}). Catch (err=>{to-do})
Instructions for Get and post requests
//Get:Axios ({method:' GET ', URL:' Http://www.zhouxiaohouer.com/api/getsth ', params:{name:' Braised chicken rice with yellow sauce ', Price:34}}). Then (res={console.log (Res.data)}).Catch(err=>{Console.log (Err)})//Post:Axios ({method:' POST ', URL:' Http://www.zhouxiaohouer.com/api/poststh ', data:{name:' Braised chicken rice with yellow sauce ', Price:34}, headers: {' Content-type ': ' application/x-www-form-urlencoded '}, Transformrequest: [function(data, headers) {//converts the data format of the data, typically returning a serialized string //a QS module is encapsulated within the Axios and can be converted directly after introduction returndata; }],}). Then (res={console.log (Res.data)}).Catch(err=>{Console.log (Err)})
Second, the alias type:
Axios.get (url[, config]) axios.post (url[, data[, Config])
With a part of the code in the Vue component
Import Axios from ' Axios 'Import QS from' Qs 'Getgoods () {var_this = This vardata ={filter:'-_id-__v ',} axios.post (' Http://www.zhouxiaohouer.com/api/poststh ', Qs.stringify (data), {headers: {' Content-type ': ' application/x-www-form-urlencoded '}}). Then (res={_this.msg=json.stringify (Res.data)}, Err={Console.log (ERR)})}
In Axios, we can also configure the default global configuration in advance
Axios.defaults.baseURL = ' Https://www.zhouxiaohouer.com/api 'axios.defaults.headers.post[' Content-type '] = ' application/x-www-form-urlencoded '
Data Interaction Axios