Laravel 5 TypeError:无法设置属性';职位';未定义-Vuejs的定义

Laravel 5 TypeError:无法设置属性';职位';未定义-Vuejs的定义,laravel-5,vuejs2,axios,Laravel 5,Vuejs2,Axios,我用VueJs和Laravel创建SPA。 主页我通过api laravel和axio数据对象获得所有帖子。 但我无法更新到posts属性 chrome调试工具中出现错误: 我在Wellcome.vue中的代码 import { mapGetters } from 'vuex' import axios from 'axios' export default { name: 'welcome', layout: 'default', metaInfo: { titleTem

我用VueJs和Laravel创建SPA。 主页我通过api laravel和axio数据对象获得所有帖子。 但我无法更新到posts属性

  • chrome调试工具中出现错误:

我在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);
    });
  },
}

您正在使用常规函数作为回调函数,这意味着
引用将发生更改。您需要在这里使用箭头函数<代码>()=>{}

 axios.get('/api/posts')
    .then((response) => {
      this.posts = response.data;
    })
    .catch((error) => {
      console.log(error);
    });

首先,您在props属性中定义了
posts
。您不应该从子组件中变异道具。道具是

您可以在数据属性中初始化
posts
,如下所示:

data(){
    return{
        posts: null
    }
}  
然后,您可以通过API获取数据,并将其分配给数据属性中的
posts

在您的
中,则
函数不指向vue实例。 所以你最好这样做

 created () {
     var vm = this;
    axios.get('/api/posts')
    .then(function (response) {
      vm.posts = response.data;
    })
    .catch(function (error) {
      console.log(error);
    });
  },
} 
 created () {
    axios.get('/api/posts')
    .then( (response) => {
      this.posts = response.data;
    })
    .catch(function (error) {
      console.log(error);
    });
  },
} 
或者您可以使用类似这样的=>函数

 created () {
     var vm = this;
    axios.get('/api/posts')
    .then(function (response) {
      vm.posts = response.data;
    })
    .catch(function (error) {
      console.log(error);
    });
  },
} 
 created () {
    axios.get('/api/posts')
    .then( (response) => {
      this.posts = response.data;
    })
    .catch(function (error) {
      console.log(error);
    });
  },
} 

我在帖子里有数据。但视图不能渲染。查看帖子:

{{post.description}

@nguyenmahlinh但是为什么要通过执行
this.posts=response.data
I通过api加载帖子来更改
posts
props的值呢,然后加载成功数据将更新到帖子并呈现到视图。我在帖子中有数据。但视图不能渲染。查看帖子:

{{post.description}}

这篇文章对我帮助很大