First, installation
1. Use NPM to install NPM installed Axios--save
2, using Bower installation bower install Axios--save
3. Direct use of CDN to introduce <script src= "Https://unpkg.com/axios/dist/axios.min.js" ></script> (direct ignore of VUE framework)
Bind (this) a lot of people like to define a that in the outside, which is a reason to point to the external this. In order to resolve the inability to access the Vue instance
. Catch error Handling
Axios cannot be used, it can only be introduced into each component that needs to send the request immediately
In order to solve this problem, there are two kinds of development ideas, one is to modify the prototype chain after the introduction of Axios, the second is to combine Vuex, encapsulating a Aciton.
Import Axios from ' Axios '
At this point, if the Axios command cannot be used in other components. But if Axios is rewritten as the prototype property of Vue, this problem can be solved
Vue.prototype. $ajax = Axios
Vue-resource does not change this point; Axios will change to save var _this=this;/var that = this;
send a GET request
//Send a request by a given ID
axios.get ('/user?id=12345 ')
. Then (function (response) {
Console.log (response);
})
. catch (function (err) {
Console.log (err);
});
//The above request can also be sent in this way
axios.get ('/user ', {
params:{
id:12345
}
})
. Then (function (response) {
Console.log (response);
})
. catch (function (err) {
Console.log (err);
});
Send a POST request
axios.post ('/user ', {
firstName: ' Fred ' ,
lastName: ' Flintstone '
})
.then (function (res) {
Console.log (res);
.catch (function (err) {
Span style= "font-size:14px" > Console.log (err);
3, one-time concurrent multiple requests
function Getuseraccount () {
Return Axios.get ('/user/12345 ');
}
function Getuserpermissions () {
Return Axios.get ('/user/12345/permissions ');
}
Axios.all ([Getuseraccount (), Getuserpermissions ()])
. Then (Axios.spread (function (acct,perms) {
This function is triggered when both requests are completed, and two parameters represent the returned result.
}))
Axios correct open mode