“TypeError: Cannot read property ‘get’ of undefined”, Axios, Vue.JS
尝试使用 axios 从 api 访问获取请求时,我从 Vue 收到这个奇怪的错误,
我收到 “TypeError: Cannot read property \\’get\\’ of undefined”
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<template>
{{ msg }} <p>Welcome to your new single-page application, built with Vue.js.</p> </template> var Vue = require(‘vue’); // import axios from”axios”; window.axios=require(‘axios’); };
<!– Add”scoped” attribute to limit CSS to this component only –> |
- 它应该只是 axios.get,而不是 this.axios.get。您可能会看到在某些示例中使用了 this.axios,但在这些情况下,它们已将 axios 存储在 this 上,而在您的代码中似乎并非如此。
- 我还建议坚持使用 import 方法,而不是使用 require 和 window。
- 谢谢@skirtle,非常感谢先生:)
this 在您的情况下不引用 window 。更好的方法是在组件中导入 axios:
1
|
import axios from ‘axios’;
|
更有效的方法是在 main.js 文件中全局设置一次:
1
|
Vue.prototype.$http = axios
|
并在任何你想要的地方使用它 this.$http
来源:https://www.codenong.com/63651860/