Home > Article > Web Front-end > Data request interceptor and global configuration of Vue and Axios
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
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
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
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
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
// main.js axios.defaults.baseURL = 'http://api.example.com' axios.defaults.timeout = 5000 Vue.prototype.$http = axios
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.
// 在组件中的某个方法中发起请求 this.$http.get('/api/data', { timeout: 10000 })
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!