“TypeError : Cannot read property 'get'of undefined”, Axios, Vue.JS

Aug 30 2020

axios를 사용하여 API에서 요청 가져 오기에 액세스하려고 할 때 Vue에서이 이상한 오류가 발생합니다. "TypeError : Cannot read property 'get'of undefined"가 표시됩니다.

 <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>


 

답변

2 MajedBadawi Aug 30 2020 at 04:21

thiswindow귀하의 경우에는 언급하지 않습니다 . 더 나은 접근 방식은 axios구성 요소에서 가져 오는 것입니다 .

import axios from 'axios';

보다 효율적인 방법은 main.js파일 에서 전역 적으로 한 번 설정하는 것입니다 .

Vue.prototype.$http = axios

그리고 원하는 곳에서 사용 this.$http