Laravel Vue+;axios返回未定义的

Laravel Vue+;axios返回未定义的,laravel,vue.js,axios,Laravel,Vue.js,Axios,我已经app.js将axios和VueAxios导入为: Vue.use(VueAxios、axios) 然后调用我的组件: Vue.component('api-call',require('./components/PostComponent') 在myPostComponent中,我有一个简单的axios,如下所示: <script> export default { // name: "PostComponent" data() {

我已经
app.js
将axios和VueAxios导入为:

Vue.use(VueAxios、axios)

然后调用我的组件:

Vue.component('api-call',require('./components/PostComponent')

在my
PostComponent
中,我有一个简单的axios,如下所示:

<script>
    export default {
        // name: "PostComponent"
        data() {
            return {
                post: {},
            }
        },
        methods: {
            getPosts: () => {
                console.log('started');
                //let that = this;
                let uri = 'https://jsonplaceholder.typicode.com/posts';
                this.axios.get(uri).then((response) => {
                        console.log(response);
                    })
            }
        },
        mounted(){
            this.getPosts()
        }
    }
</script>

导出默认值{
//名称:“后组件”
数据(){
返回{
职位:{},
}
},
方法:{
getPosts:()=>{
log('started');
//让那=这;
让我来看看https://jsonplaceholder.typicode.com/posts';
this.axios.get(uri).then((响应)=>{
控制台日志(响应);
})
}
},
安装的(){
这是getPosts()
}
}
因为我希望在组件加载开始时就执行此操作,所以我使用的是装入的
模式(为什么Vue没有构造函数,甚至是在装入的
模式上传递的react都让我感到困惑。)

我做错了什么

谢谢,
Bud

您不能将箭头函数用于方法声明

请注意,不应使用箭头函数来定义方法 (例如plus:()=>this.a++)。原因是箭头函数绑定了 父上下文,因此这将不是您期望的Vue实例,并且 此.a将是未定义的

以下是正确定义方法的两种方法

  • (如果您可以使用ES6)


  • 错误消息是什么?
        getPosts: function() {
    
        }
    
        getPosts() {
    
        }