This time I will bring you the vue interceptor compatibility process. What are the precautions when using the vue interceptor compatibility? The following is a practical case, let's take a look.
In the project, vue is used to build the front-end page, and the back-end API interface is requested through axios to complete data interaction. If the verification password token is written in each interface, it will be a lot of manual work and not flexible. Here we share the use of vue's built-in interceptor to add a token to the header of each request, and it is compatible with IE9.
import?axios?from?'axios'; //?這里的config包含每次請(qǐng)求的內(nèi)容,在這里把token放到請(qǐng)求頭 axios.interceptors.request.use(function?(config)?{? ??let?token?=?window.localStorage.getItem("tokenid");?//從緩存中取token ??if?(token)?{ ????config.headers.Authorization?=?token;??//將token放到請(qǐng)求頭發(fā)送給服務(wù)器 ????//這里主要是為了兼容IE9 ????var?browser?=?navigator.appName; ????var?b_version?=?navigator.appVersion; ????if?(browser?==?'Netscape'?&&?b_version.indexOf(';')?< 0) { //火狐 } else { if (b_version.indexOf(';') < 0) { return config; } var version = b_version.split(";"); var trim_Version = version[1].replace(/[ ]/g, ""); if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE9.0") { //IE9 if (config.url.indexOf('?') >?0)?{ ??????????config.url?=?config.url?+?"&token="?+?JSON.parse(token).value; ????????} ????????else?{ ??????????config.url?=?config.url?+?"?token="?+?JSON.parse(token).value; ????????} ??????} ????} ??}?else?{ ????localStorage.clear();?//清空緩存 ????if?(router.currentRoute.name?&&?router.currentRoute.name.toLowerCase()?==?"login")?{? ?? //這里需要排除登陸(或者說(shuō)是第一次請(qǐng)求獲取token)的時(shí)候的請(qǐng)求驗(yàn)證,我這里沒(méi)做處理 ?//我的后臺(tái)api接口,并沒(méi)有對(duì)login接口做token驗(yàn)證,所以這里不做處理 ????}?else?{?? ?? //除登陸接口外,其他需要token驗(yàn)證的方法,會(huì)走這里且返回null ??????return?null; ????} ??} ??return?config; },?function?(err)?{ ??//?return?Promise.reject(err); }); //?http?response?攔截器 axios.interceptors.response.use( ??response?=>?{ ????return?response;?//請(qǐng)求成功的時(shí)候返回的data ??}, ??error?=>?{ ????try?{ ??????if?(error.response)?{ ????????switch?(error.response.status)?{ ??????????case?401://token過(guò)期,清除token并跳轉(zhuǎn)到登錄頁(yè)面 ????????????localStorage.clear(); ????????????var?baurl?=?window.location.href; ???????? router.replace({ ????????????????path:?'login', ????????????????query:?{?backUrl:?baurl?} ??????????????});??????????? ????????????return; ????????} ??????} ??????return?Promise.reject(error.response.data) ????} ????catch?(e)?{ ????} ??});
Write it at the back. Because my token is placed in the cache, before each request, I will first take out the token on the front end and verify its timeliness. If it expires or does not exist, I will jump to the login page first without going through the interceptor. Go request request. Please also refer to the mounted() method for details.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of utils.js use cases
The above is the detailed content of vue interceptor compatibility processing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Nowadays, many mobile phones claim to support Bluetooth 5.3 version, so what is the difference between Bluetooth 5.3 and 5.2? In fact, they are essentially subsequent updated versions of Bluetooth 5, and there is not much difference in most performance and functions. The difference between Bluetooth 5.3 and 5.2: 1. Data rate 1 and 5.3 can support higher data rates up to 2Mbps. 2. While 5.2 can only reach a maximum of 1Mbps, it means that 5.3 can transmit data faster and more stably. 2. Encryption control enhancement 2. Bluetooth 5.3 improves encryption key length control options, improves security, and can better connect to access control and other devices. 3. At the same time, because the administrator control is simpler, the connection can be more convenient and faster, which is not possible in 5.2.

The performance of i77700 is completely sufficient to run win11, but users find that their i77700 cannot be upgraded to win11. This is mainly due to restrictions imposed by Microsoft, so they can install it as long as they skip this restriction. i77700 cannot be upgraded to win11: 1. Because Microsoft limits the CPU version. 2. Only the eighth generation and above versions of Intel can directly upgrade to win11. 3. As the 7th generation, i77700 cannot meet the upgrade needs of win11. 4. However, i77700 is completely capable of using win11 smoothly in terms of performance. 5. So you can use the win11 direct installation system of this site. 6. After the download is complete, right-click the file and "load" it. 7. Double-click to run the "One-click

The software in the win10 system has been perfectly optimized, but for the latest win11 users, everyone must be curious about whether this system can be supported, so the following is a detailed introduction to the win11 software that does not support win10. Come and find out together. Does win11 support win10 software: 1. Win10 system software and even Win7 system applications are well compatible. 2. According to feedback from experts who use the Win11 system, there are currently no application incompatibility issues. 3. So you can upgrade boldly with confidence, but ordinary users are advised to wait until the official version of Win11 is released before upgrading. 4. Win11 not only has good compatibility, but also has Windo

More and more users are starting to upgrade the win11 system. Since each user has different usage habits, many users are still using the ie11 browser. So what should I do if the win11 system cannot use the ie browser? Does windows11 still support ie11? Let’s take a look at the solution. Solution to the problem that win11 cannot use the ie11 browser 1. First, right-click the start menu and select "Command Prompt (Administrator)" to open it. 2. After opening, directly enter "Netshwinsockreset" and press Enter to confirm. 3. After confirmation, enter "netshadvfirewallreset&rdqu

1. Right-click the program and find that the [Compatibility] tab is not found in the properties window that opens. 2. On the Win10 desktop, right-click the Start button in the lower left corner of the desktop and select the [Run] menu item in the pop-up menu. 3. The Win10 run window will open, enter gpedit.msc in the window, and then click the OK button. 4. The Local Group Policy Editor window will open. In the window, click the [Computer Configuration/Administrative Templates/Windows Components] menu item. 5. In the opened Windows component menu, find the [Application Compatibility] menu item, and then find the [Remove Program Compatibility Property Page] setting item in the right window. 6. Right-click the setting item, and in the pop-up menu

With the continuous development of modern technology, wireless Bluetooth headsets have become an indispensable part of people's daily lives. The emergence of wireless headphones frees our hands, allowing us to enjoy music, calls and other entertainment activities more freely. However, when we fly, we are often asked to put our phones in airplane mode. So the question is, can I use Bluetooth headphones in airplane mode? In this article, we will explore this question. First, let’s understand what airplane mode does and means. Airplane mode is a special mode for mobile phones

The Go language has very good compatibility on Linux systems. It can run seamlessly on various Linux distributions and supports processors of different architectures. This article will introduce the compatibility of Go language on Linux systems and demonstrate its powerful applicability through specific code examples. 1. Install the Go language environment. Installing the Go language environment on a Linux system is very simple. You only need to download the corresponding Go binary package and set the relevant environment variables. Following are the steps to install Go language on Ubuntu system:

Best practices to solve PHP function compatibility issues: Use versioned function names (for example: array_map_recursive()) Leverage function aliases (for example: functionarray_map($callback,$array){...}) to check function availability (for example: if (function_exists('array_map_recursive')){...}) use namespace (for example: namespaceMyNamespace{...})
