Ngu*_*inh 2 laravel-5 axios vuejs2
我使用VueJs和Laravel创建SPA。我通过api laravel和axio响应获得了所有帖子的主页数据对象。但是我无法更新到职位属性。
我在Wellcome.vue中的代码
import { mapGetters } from 'vuex'
import axios from 'axios'
export default {
name: 'welcome',
layout: 'default',
metaInfo: { titleTemplate: 'Welcome | %s' },
computed: mapGetters({
authenticated: 'authCheck'
}),
data: () => ({
title: 'Demo Blog',
}),
props: {
posts: {
type: Object
}
},
created () {
axios.get('/api/posts')
.then(function (response) {
this.posts = response.data;
})
.catch(function (error) {
console.log(error);
});
},
}
Run Code Online (Sandbox Code Playgroud)
您正在使用常规函数作为回调,这意味着this引用发生了变化。您需要在此处使用箭头功能。() => {}。
axios.get('/api/posts')
.then((response) => {
this.posts = response.data;
})
.catch((error) => {
console.log(error);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1843 次 |
| 最近记录: |