如何解决“ TypeError:无法读取未定义的属性'get'”,Axios,Vue.JS
当尝试使用axios从api访问get请求时,我从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>
解决方法
this
在您的情况下未引用window
。更好的方法是将axios
导入组件:
import axios from 'axios';
一种更有效的方法是在main.js
文件中全局设置一次:
Vue.prototype.$http = axios
并以this.$http
的身份随处使用它
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。