Application of Axios

Source: Internet
Author: User

After Vue has been updated to 2.0, the author declares that it is no longer a vue-resource update, but rather the recommended Axios, which has been used for a while, and now for its basic usage.

The first is to introduce Axios, if you use ES6, just install the Axios module

Import Axios from ' Axios ';
Installation method
NPM Install Axios
Or
Bower Install Axios

Of course, you can also use script to introduce

<script src= "Https://unpkg.com/axios/dist/axios.min.js" ></script>

Axios provides several ways of requesting

Axios.request (config) axios.get (url[, config]) Axios. Delete(url[, config]) axios.head (url[, config]) axios.post (url[, data[, config]) axios.put (url[, data[, config ]]) Axios.patch (url[, data[, config]) 

Config here is the configuration of some basic information, such as the request header, BaseURL, of course, here are some of the more convenient configuration items

Config
Import qs from ' QS '
{//The requested interface, when requested, such as Axios.get (Url,config), where the URL will overwrite the URL url in config: '/user ',//Request method: ' Get ',//Default//Base URL prefix baseURL: ' https://some-domain.com/api/',
  
Transformrequest: [function(data) {//Here can be sent before the request data processing, such as Form-data format, which can be used at the beginning of the introduction of the Qs (this module has been installed Axios when installed, do not need to install) data = Qs.stringify ({});Return data;}], transformresponse: [function// Here the returned data is processed in advance return data;}], // request header information headers: {' x-requested-with ': ' XMLHttpRequest ' },/ /parameter parameter params: {id:12345 data: {firstName: ' Fred ' // default }

With the configuration file, we can reduce a lot of extra processing code and be more graceful, directly using

Axios.post (Url,{},config)    . Then (function(res) {        console.log (res);    })    . catch (function(err) {Console.log (err);})
Axios request returned is also a promise, tracking error only need to add a catch on the last.
Here's how to handle multiple requests at the same time
Axios. All ([Get2 ()])  .  Then (Axios.  Spread (function (//Only two requests are completed to succeed, otherwise caught by Catch}));      

Finally, the configuration item, above is the additional configuration, if you do not want to write another can directly configure the global

Axios.defaults.baseURL = ' https://api.example.com '; axios.defaults.headers.common[' Authorization '] = AUTH _token;axios.defaults.headers.post[' content-type '] = ' application/x-www-form-urlencoded ';

Of course, it can be configured like this
Axios. Create ({  BaseURL' https://api.example.com'});  

This article is just about basic usage, see the Official document Https://github.com/axios

I have written two examples:

Using Vue2.0+mintui+axios+vue-router:https://github.com/stevenzwzhai/vue-mobile-application

Using Vue2.0+elementui+axios+vue-router:https://github.com/stevenzwzhai/vue2.0-elementui-axios-vuerouter, In this example, the PWA file is simply configured with some things that can be used for PWA application Development Reference, article reference http://www.cnblogs.com/Upton/p/6894589.html

Application of Axios

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.