Simple package of Axios and use within the assembly

Source: Internet
Author: User

/** First Step * Configure the switch between the compilation environment and the on-line environment * BASEURL: Domain name address * Routermode: Route mode * Imgbaseurl: The domain name where the picture is located **/Let Host= ' '; let Routermode= ' Hash ';//Let Imgbaseurl;if(Process.env.NODE_ENV = = ' development ') {Host= ' http://test.xxx.com ';//test Environment Host    //imgbaseurl = ' http://cangdu.org:8001/img/'; If the picture needs to be placed on a third-party platform, you can change the network path} Else if(Process.env.NODE_ENV = = ' Production ') {Host= ' http://xxx.com ';//Production Environment Host    //imgbaseurl = ' http://cangdu.org:8001/img/'; If the picture needs to be placed on a third-party platform, you can change the network path}export {Host, Routermode//Imgbaseurl,}
//Second step
Import {Host} from './env '; import Vue from' Vue '; import Axios from' Axios '; import {toastplugin, alertplugin} from' Vux '; import Qs from' Qs ';//axios.defaults.timeout = 5000;//Response TimeAxios.interceptors.request.use (function(config) {//do something before sending a request returnconfig;},function(Error) {//request a mistake to do something returnPromise.reject (Error);}); Axios.interceptors.response.use (function(response) {//do some processing on the response data returnresponse;},function(Error) {//do some processing on response failure returnPromise.reject (Error);});//get PackageExportfunctionGetajax (URL, params = {}) { return NewPromise (Resolve, reject) ={axios.get (URL, {params:params}). Then (response={Resolve (Response.data); }) .Catch(Err ={reject (err) })})}//post PackageExportfunctionPostajax (URL, data = {}) { return NewPromise (Resolve, reject) ={axios.post (URL, qs.stringify (data), {headers: {' X-requested-with ': ' XMLHttpRequest ', ' Content-type ': ' application/x-www-form-urlencoded ', } }) . Then (response={Resolve (Response.data); }, Err={reject (err) })})}
//Step three
Import {Host} from './env '; import {getajax, postajax} from'./http 'Import {Formatparam} from'./mutils 'Import Qs from' Qs ';//sign up for bookingExportfunctionsignupadvance (Phonenum, code) {Let Data={phone:phonenum, Captcha:code,} const RES4= Postajax (' ${host}/website/login ', data); returnres4;}//SMS Fill outExportfunctionGetCode (pages) {Let params={page:pages} const RES6= Getajax (' ${host}/website/banner/?${formatparam (params)} ');returnres6;}
//Fourth step (for use in components)
<script>Import {getcode, signupadvance} from‘.. /config/api 'Exportdefault{data () {return{}}, created () {
//get request GetCode (' Home '). Then (res ={console.log (res); }).Catch(Err ={Console.log (ERR); })}, mounted () {
//post Request signupadvance (' 15220074083 ', ' 400220 '). Then (res ={console.log (res); }).Catch(Err ={console.log (err); }) }}</script>

Simple package of Axios and use within the assembly

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.