Axios correct open mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.