Home > Web Front-end > Vue.js > body text

Data request interceptor and global configuration of Vue and Axios

WBOY
Release: 2023-07-18 08:57:09
Original
1983 people have browsed it

Data request interceptor and global configuration of Vue and Axios

1. Introduction
During the development process of the Vue project, we often use the Axios library to make data requests. Axios provides the functions of request interceptor and response interceptor, which can preprocess requests and responses to enhance the flexibility and security of the project. This article will introduce how to use the data request interceptor and global configuration of Vue and Axios to implement global request configuration and processing.

2. Data request interceptor

  1. The role of the request interceptor
    The request interceptor intercepts before sending the request, and can perform some processing on the request, such as adding request headers , add authentication, etc. Usually, we will use request interceptors to add some global configuration.
  2. Configuring the request interceptor in the Vue project
    In the Vue project, we can configure the request interceptor through the interceptors property of Axios. The sample code is as follows:
// main.js

import axios from 'axios'

// 请求拦截器
axios.interceptors.request.use(function (config) {
  // 进行一些处理,例如添加请求头、身份验证等
  config.headers['Authorization'] = 'Bearer ' + localStorage.getItem('token')
  return config
}, function (error) {
  return Promise.reject(error)
})

Vue.prototype.$http = axios
Copy after login

In the above code, we added a request header Authorization in the request interceptor, and added the token value returned by the background to the request header . In this way, authentication information will automatically be brought when sending a request.

3. Data response interceptor

  1. The role of response interceptor
    The response interceptor intercepts after obtaining the response, and can perform some processing on the response, such as handling errors. information, format the returned data, etc. Usually, we use response interceptors to handle some global error information.
  2. Configuring the response interceptor in the Vue project
    In the Vue project, we can also configure the response interceptor through the interceptors property of Axios. The sample code is as follows:
// main.js

// 响应拦截器
axios.interceptors.response.use(function (response) {
  return response
}, function (error) {
  // 处理一些错误信息
  if (error.response) {
    // 根据错误状态码进行处理
    switch (error.response.status) {
      case 401:
        // 处理未授权的情况
        break
      case 403:
        // 处理权限不足的情况
        break
      case 500:
        // 处理服务器错误的情况
        break
      // ...
    }
  }
  return Promise.reject(error)
})

Vue.prototype.$http = axios
Copy after login

In the above code, we handle some common error status codes in the response interceptor and perform corresponding processing according to different status codes. In this way, error information can be processed uniformly when an error occurs.

4. Global configuration

  1. Configuring the global default value of Axios
    In addition to using interceptors to process requests and responses, we can also configure the global default value of Axios To implement some global configuration. The sample code is as follows:
// main.js

axios.defaults.baseURL = 'http://api.example.com'
axios.defaults.timeout = 5000

Vue.prototype.$http = axios
Copy after login

In the above code, we configure the global default value of Axios, where baseURL represents the base URL of the request and timeout represents The request timeout.

  1. Special configuration for individual requests
    In addition to global configuration, we can also perform special configuration in individual requests to override the global default value. The sample code is as follows:
// 在组件中的某个方法中发起请求
this.$http.get('/api/data', {
  timeout: 10000
})
Copy after login

In the above code, we override the global default timeout by passing a special configuration in the request.

5. Summary
Through the data request interceptor and global configuration of Vue and Axios, we can preprocess requests and responses to enhance the flexibility and security of the project. We can implement some global configuration and processing through interceptors, such as adding request headers, handling error messages, etc. At the same time, we can also meet different needs through global configuration and special configuration. In actual development, we can flexibly use these functions according to actual conditions to improve development efficiency and code quality.

The above is the detailed content of Data request interceptor and global configuration of Vue and Axios. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template