Simple use of Axios in Vue project

Source: Internet
Author: User

Axios Introduction

Axios is a promise-based HTTP library that can be used in browsers and node. js
Official Warehouse: Https://github.com/axios/axios
English Document: https://www.kancloud.cn/yunye/axios/234845

The use of Axios in the project (VUE)

There is no Vue project to use VUE-CLI scaffolding to generate a webpack template of the project can be a pleasant look down ~
If development encounters cross-domain issues, refer to: http://www.cnblogs.com/morang/p/8423763.html

Installing Axios into a project

npm install axios --save

Configure Wepack aliases to access different configuration interfaces in different environments

Configuration:

Use:import config from ‘config‘

Encapsulates a Axios instance

New fetch.js , creating Axios instances and filters here

If the agent is configured. Then config.apibaseurl configure the proxy prefix to

import  config from   ' config '  import  axios from   ' Axios '  //import Qs from ' QS ';  const  instance =  axios . create  ({ baseurl  :  config . apibaseurl      //API Base_url  timeout  :  10000                    //request time-out time  //Transformrequest:data = qs.stringify (data)   )   

Axios The default submission format is: application/json , after the conversion is submitted in the formatapplication/x-www-form-urlencoded
in ASP. NET core, you need to add data to the action method [FromBody] that receives the JSON format, which is normal for application/x-www-form-urlencoded this modification.
qsyou can convert data format types by installing them into your project as you use them
npm install qs --save

Using the QS Conversion request comparison Chart

Adding interceptors to instances
//Add request blockerinstance.Interceptors.Request. Use(function(config){    //What to do before sending a request    returnConfig;  }, function(Error){    //What to do with the request error    return Promise.Reject(Error);  });//Add response blockerinstance.Interceptors.Response. Use(function(response){    //Do something about the response data    returnResponse;}, function(Error){    //Do something about the response error    return Promise.Reject(Error);}); //Last exposed instanceExport defaultInstance
Invocation of the instance

If express proxy is configured, the request path is: Browser->express Development Server-----Send request----> interface server

ImportFetch from ' Fetch.js '//getFetch({    URL:'/home/data ',//The full request path is configured for Fetch.js baseurl+/home/data?pageindex=1    Method:' GET ',    params:{PageIndex:1}})//postFetch({    BaseURL:'/api/v1 ',//The full request path is/api/v1/home/save    URL:'/home/save ',    Method:' POST ',    Data:{ID:1}})

Simple use of Axios in Vue project

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.