🗒️Vue3 与 Axios 跨域问题
Vue|2024-3-16|最后更新: 2024-5-31
type
status
date
slug
summary
tags
category
icon
password
😀
由于 vue2.x 之前存在 .this 获取上下文,在新的 Vue3 版本中,<script> 已经使用语法糖格式,不存在 .this 需要使用 Composition API 中的 getCurrentInstance 方法实现获取 mian.js 中的全局配置

📝 axios 在 Vue3 中的全局配置

全局配置

在 vue 的项目根目录下面找到 mian.js
notion image
注意这里的全局配置只适用与 Vue3,Vue2.x 不适用

在 .vue 页面中使用 axios 方法实现跨域

  1. 使用 Vue3 的语法糖格式
  1. 在组件被成功挂载之后,使用 Vue 的生命周期函数回调 axios get方法访问后端的 Json 数据
  • 方法一:const { proxy } = getCurrentInstance();
  • 方法二:将如下代码替换之前的 const { proxy } = getCurrentInstance(); 即可

📎 参考文章

Redis 报错使用 AOP
Loading...