【发布时间】:2020-12-18 10:54:54
【问题描述】:
尝试使用 axios 从 api 访问获取请求时,我从 Vue 收到这个奇怪的错误, 我收到“TypeError:无法读取未定义的属性 'get'”
<template>
<div class="home">
<h1>{{ msg }}</h1>
<p>Welcome to your new single-page application, built with <a href="https://vuejs.org" target="_blank">Vue.js</a>.</p>
</div>
</template>
<script>
var Vue = require('vue');
// import axios from "axios";
window.axios=require('axios');
export default {
name: 'Home',
props: {
msg: String
},
component: {
},
mounted: function () {
alert('Hi ');
this.axios.get('https://api.github.com/users')
.then(value => {
alert(value);
});
}
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
【问题讨论】:
-
应该只是
axios.get,而不是this.axios.get。您可能会看到在某些示例中使用了this.axios,但在这些情况下,它们已将axios存储在this上,而您的代码中似乎并非如此。 -
我还建议坚持使用
import方法,而不是使用require和window。 -
谢谢@skirtle,非常感谢先生:)
标签: vue.js vuejs2 axios vue-component vuex