Home Web Front-end JS Tutorial vue+axios makes login request interception

vue+axios makes login request interception

Apr 20, 2018 pm 02:10 PM
intercept ask

This time I will bring you vue axios production login request interception. What are the precautions for vue axios production login request interception. Here is a practical case, let's take a look.

When we are making interface requests, such as when judging login timeout, the interface usually returns a specific error code. Then if we judge a time-consuming and labor-consuming interface for each interface, we can use The interceptor performs unified http request interception.

1. Install and configure axios

cnpm install --save axios

We can build A js file is used for this unified processing. Create a new axios.js, as follows

import axios from 'axios' 
import { Indicator } from 'mint-ui'; 
import { Toast } from 'mint-ui'; 
// http request 拦截器 
axios.interceptors.request.use( 
  config => { 
    Indicator.open() 
    return config; 
  }, 
  err => { 
    Indicator.close() 
    return Promise.reject(err); 
  }); 
// http response 拦截器 
axios.interceptors.response.use( 
  response => { 
    Indicator.close() 
    return response; 
  }, 
  error => { 
    Indicator.close() 
  }); 
export default axios
Copy after login

Then introduce this js file into main.js

import axios from './axio'; 
Vue.prototype.$axios = axios;
Copy after login

so that you can use axios to request. In the component, you can use this.axios to call

this.$axios({ 
    url:requestUrl+'homePage/v1/indexNewPropertiesResult', 
    method:'POST', 
   }).then(function(response){ //接口返回数据 
    console.log(response) 
    that.modulesArr=response.data.data.modules; 
//   that.getRecommendGoods(0); 
   });
Copy after login

Only by using axios to request the interface, you can intercept it. Now it can be intercepted in axios.js, and you can do the operations you need in the two states

Supplement:

axios uses interceptors to process all http requests uniformly

axios uses interceptors

Intercept requests or responses before they are processed by then or catch.

•http request interceptor

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  return config;
 }, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
 });
Copy after login

•http responses interceptor

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response;
 }, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
 });
Copy after login

•Remove interceptor

var myInterceptor = axios.interceptors.request.use(function () {/*...*/});
axios.interceptors.request.eject(myInterceptor);
Copy after login

•Add interceptor for custom axios instance

var instance = axios.create();
instance.interceptors.request.use(function () {/*...*/});
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting things, please pay attention to php Chinese Other related articles online!

Recommended reading:

Detailed explanation of Vue project packaging steps by environment

Avoid Dom misunderstandings when using Angular2

The above is the detailed content of vue+axios makes login request interception. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make cross-domain requests in Vue? How to make cross-domain requests in Vue? Jun 10, 2023 pm 10:30 PM

Vue is a popular JavaScript framework for building modern web applications. When developing applications using Vue, you often need to interact with different APIs, which are often located on different servers. Due to cross-domain security policy restrictions, when a Vue application is running on one domain name, it cannot communicate directly with the API on another domain name. This article will introduce several methods for making cross-domain requests in Vue. 1. Use a proxy A common cross-domain solution is to use a proxy

How to delete all strangers' messages in Kuaishou private messages? Can private messages from strangers be intercepted? How to delete all strangers' messages in Kuaishou private messages? Can private messages from strangers be intercepted? Mar 22, 2024 am 08:50 AM

Kuaishou is a popular short video social platform that allows users to easily connect with others. Over time, users' private messages may be filled with a large number of strangers' messages, which may affect the user's experience. So, how to delete private messages from strangers on Kuaishou? This article will introduce in detail how to delete private messages from strangers on the Kuaishou platform, and whether it is possible to intercept messages from strangers. 1. How to delete all strangers’ messages in Kuaishou private messages? 1. First, open Kuaishou APP and enter the personal center. 2. On the personal center page, find the "Message" option and click to enter. 3. On the message page, find the "Private Message" option and click to enter. 4. On the private message page, you can see different message categories. Find the "Stranger Messages" category and click

How to block advertising pop-ups in QQ browser How to block advertising pop-ups in QQ browser Jan 31, 2024 pm 06:00 PM

How to block advertising pop-ups in QQ browser? Recently, sometimes when I use the computer, I often encounter the phenomenon of advertising pop-ups in the QQ browser. Like what I encountered is the QQ browser pop-up advertising, so when I encounter this kind of QQ browser pop-up advertising How to solve it? Let’s take a look with the editor of this site to see how to block advertising pop-ups in QQ browser. Tutorial to solve QQ browser pop-up ads 1. First open QQ browser, enter the main interface, and click the menu in the upper right corner. 2. After clicking on the menu of QQ Browser, you will see an application center, and then click on it. 3. After entering the QQ Browser Application Center, an extension store will pop up. 4. Install the QQ browser plug-in to block advertising pop-ups. 5. Click Install Now. 6. Install it into

How Nginx implements request rewrite configuration based on request URL How Nginx implements request rewrite configuration based on request URL Nov 08, 2023 pm 04:15 PM

Nginx is a lightweight, high-performance web server that not only supports advanced functions such as reverse proxy and load balancing, but also has powerful request rewriting capabilities. In actual web applications, in many cases the request URL needs to be rewritten to achieve better user experience and search engine optimization effects. This article will introduce how Nginx implements request rewriting configuration based on the request URL, including specific code examples. Rewrite syntax In Nginx, you can use the rewrite directive to perform request rewriting. its basic language

Common application scenarios of the Head request method in Laravel Common application scenarios of the Head request method in Laravel Mar 06, 2024 pm 09:33 PM

Common application scenarios of the Head request method in Laravel In Laravel, the HEAD method in the HTTP request method is usually used to obtain the metadata of the resource without obtaining the actual content. The HEAD request is similar to the GET request, but does not return the actual response body content, only the response header information. This makes the HEAD request very useful in some specific scenarios. The following are some common application scenarios and corresponding code examples. Verify the validity of the link using the HEAD request method can be used to verify the chain

How to use context to implement request retry strategy in Go How to use context to implement request retry strategy in Go Jul 21, 2023 pm 04:39 PM

How to use context to implement request retry strategy in Go Introduction: When building a distributed system, network requests will inevitably encounter some failures. In order to ensure the reliability and stability of the system, we usually use a retry strategy to handle these failed requests to increase the success rate of the request. In the Go language, we can use the context package to implement the request retry strategy. This article will introduce how to use the context package in Go to implement a request retry strategy, with code examples. 1. What is

How to use context to implement request idempotence in Go How to use context to implement request idempotence in Go Jul 21, 2023 pm 12:37 PM

How to use context to implement request idempotence in Go Introduction In web development, idempotence is a very important concept. It ensures that multiple executions of a request do not have inconsistent side effects on the system. When dealing with concurrent requests or when the network is unstable, using idempotence can ensure the security and reliability of requests. The context package in Go provides a mechanism to handle this situation. What is idempotence? Simply put, idempotence refers to the result of multiple executions of the same operation being the same as the result of one execution.

How to use Hyperf framework for request retries How to use Hyperf framework for request retries Oct 24, 2023 am 09:37 AM

How to use the Hyperf framework for request retry. With the unpredictability of network communication, we often encounter request failures in application development. In order to ensure the stability and robustness of the application, we can increase the success rate of requests through the request retry mechanism. In the Hyperf framework, we can use the Retry component provided by Hyperf to implement the request retry function. This article will introduce in detail how to use the Retry component in the Hyperf framework and give specific code examples. First, we need to

See all articles