Using Axios in Vue.js

Source: Internet
Author: User

Axios is the official recommended HTTP request tool for vue2.0, before the Vue-resource

Two ways of using it are summarized in the process:

1. Use similar with Vue-resource

Introduced:import axios from ‘axios‘;

       Vue.prototype.$http = axios;

Use:this.$http.get(URL).then(response => { // success callback }, response => { // error callback });

2. Use in the components you need

introduced: import Axios from ' Axios ';

use: < Span class= "Hljs-keyword" > axios . Get (URL). Then (< Span class= "Hljs-params" >response = {//success Callback}, response = {//Error callback });

Note: Assigning the response return value to data is not always successful, the previous code:

axios.get ()
if (Response.data.errno = = = Err_ok) {
Span style= "COLOR: #736f5c" > This.seller = Response.data.data;
}
Console.log (response.data.data);
})
. catch (function (error) {
Console.log (error);
});

Now instead of the ES6 syntax specification, now the code:

     Axios. Get ('/api/seller '). Then (response = {
if (Response.data.errno = = = Err_ok) {
This.seller = Response.data.data;
}
}, Response = {
Console.log (response);
});

Using Axios in Vue.js

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.