vue前台保存token
时间: 2023-08-28 12:10:53 浏览: 115
在Vue前端中,可以使用localStorage或者vuex来保存token。具体的实现思路如下:
1. 在用户登录成功后,后端会返回一个token给前端。
2. 前端可以将这个token存储到localStorage中,使用localStorage.setItem('token', token)来保存。
3. 另外,也可以将token存储到vuex中,通过vuex的state来管理token的状态。
4. 在每次跳转路由或者发送请求时,可以从localStorage或者vuex中获取token,并将其添加到请求头中,以便后端进行验证。
5. 如果后端返回的状态码为401,表示token验证失败,前端可以清除localStorage中的token,并跳转到登录页面。
总结起来,前端可以使用localStorage或者vuex来保存token,并在需要的时候获取并使用token进行验证和请求。
#### 引用[.reference_title]
- *1* *3* [Vue项目中实现用户登录及token验证](https://blog.csdn.net/m0_37911706/article/details/125316373)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [Vue项⽬中 登录时 对于token的处理](https://blog.csdn.net/m0_67841039/article/details/125024185)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)