vue用axios解决跨域问题遇到:Cannot read property ‘get‘ of undefined!

博客作者最初在Vue项目中遇到axios导入问题,错误地采用了网上的错误解决方案。经过搜索,找到了正确的引入方式,即通过将axios挂载到Vue原型上实现。正确的代码片段为:import axios from 'axios'; Vue.prototype.$axios = axios。

一开始我是忘记引入axios了,就在网上搜了一下,搜到的答案如下(是错误的,是错误的,是错误的!!)

import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueAxios, axios)

引入之后还是报错,就又去搜了一下怎么解决的问题
后来发现正确的写法是这样的

import axios from 'axios'
Vue.prototype.$axios = axios
Tb.vue:29 TypeError: Cannot read properties of undefined (reading '_wrapper') at remove (vue.runtime.esm.js:7090:11) at updateListeners (vue.runtime.esm.js:1876:7) at updateDOMListeners (vue.runtime.esm.js:7102:3) at Array.destroy (vue.runtime.esm.js:7110:12) at invokeDestroyHook (vue.runtime.esm.js:6388:62) at invokeDestroyHook (vue.runtime.esm.js:6392:9) at VueComponent.patch [as __patch__] (vue.runtime.esm.js:6698:28) at Vue.$destroy (vue.runtime.esm.js:3720:8) at destroy (vue.runtime.esm.js:4316:27) at invokeDestroyHook (vue.runtime.esm.js:6387:57) <template> <div class="download-component"> <button class="download-button" @click="downloadZip">下载压缩包</button> </div> </template> <script> import axios from 'axios'; export default { name: 'TbComponent', methods: { downloadZip() { axios({ url: 'http://10.70.19.171:8889/ax/download?id=100', method: 'GET', responseType: 'blob', // 设置响应类型为 blob }) .then((response) => { const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', 'downloaded_file.zip'); // 设置下载文件名 document.body.appendChild(link); link.click(); document.body.removeChild(link); }) .catch((error) => { console.error('下载失败:', error); }); }, }, }; </script> <style scoped> .download-component { display: flex; justify-content: center; align-items: center; height: 100vh; /* 让组件在垂直方向上占据整个视口高度 */ } .download-button { padding: 12px 24px; font-size: 16px; font-weight: bold; color: white; background-color: #007BFF; border: none; border-radius: 4px; cursor: pointer; transition: background-color 0.3s ease; } .download-button:hover { background-color: #0056b3; } .download-button:active { background-color: #003d80; } </style>
09-16
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值