Axios Configuration request interception function and request method in Vue how to encapsulate

Source: Internet
Author: User

In Main.js:

 from ' ... ......./axios '

In Axios.js:

//Axios.jsImport Vue from 'Vue'Import Axios from 'Axios'Vue.prototype. $http=Axios//HTTP request Encapsulation Header InterceptorAxios.interceptors.request.use (config = {  //Console.log ("request")//console.log (config)//Request MethodLet method =config.method.toLowerCase (); if(Method = = ='Get'|| method = = ='Delete') {object.assign (config.params, {      "Test":"TestVal"    }); }  returnconfig;}, error= {  returnPromise.reject (err);});//HTTP response Encapsulation back-end InterceptorAxios.interceptors.response.use (response = {  //Console.log (response)//REDIRECT to login page when return information is not logged in or invalid login//if (Response.data.code = = ' w_100004 ' | | response.data.message = = ' User is not logged in or login timeout, please login! ') {//Router.push ({//path: "/",//Querry: {//Redirect:router.currentRoute.fullPath//       } //from which page to jump//     })  //   }  if(typeofResponse.data = = ='string') {    returnJson.parse (Response.data); } Else {    returnresponse; }}, Error= {  returnpromise.reject (Error)});

Use:

this. $http. Get ('/api/ ... ', {params: {}}). Then (res = {                console.log (res)            + =                {   Error callback            });

Axios Configuration request interception function and request method in Vue how to encapsulate

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.