/** 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