Vue.js 数据 : undefined

标签 vue.js

我是 Vue.js 的新手。

请多多指教。
我收到 comments: undefined 所以评论没有显示。 xhr带200是正常的。

谢谢 谢谢 谢谢 谢谢 谢谢

<template>
 <div>
  <ul class="media-list">
     <li class="media" v-for="comment in comments">
         {{ $comment.body }}            
     </li>
  </ul> 
</div>
</template>


<script>
 export default {
    data () {
        return {
            comments: []
        }
    },
    props: {
        postid: null
    },
    methods: {
        getComments () {
            this.$http.get('/blog/' + this.postid + '/comments').then((response) => {
                this.comments = response.json().data;
            });
        }
    },
    mounted () {
        this.getComments();
    }
}

最佳答案

基本上有两个问题:

  1. $comment不存在
  2. 您在 response.json().data 上没有数据,这就是您得到 undefined 的原因

我使用不同的 API 只是为了测试它(因为我无权访问您的 API)。

模板

<div id="app">
  <ul class="media-list">
    <li class="media" v-for="comment in comments">
      {{ comment.familyName + ', ' + comment.givenName }}
    </li>
  </ul>
</div>

脚本

new Vue({
    el: '#app',
  data () {
    return {
      comments: []
    }
  },
  props: {
    postid: null
  },
  methods: {
    getComments () {
      this.$http.get('//ergast.com/api/f1/drivers.json').then((response) => {
        this.comments = response.body.MRData.DriverTable.Drivers;
      });
    }
  },
  mounted () {
    this.getComments();
  }
});

查看工作示例 here

关于Vue.js 数据 : undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44228843/

相关文章:

javascript - 如何将 vue-router 数据获取到父模板中?

javascript - VueJS : Delete multiple tasks. 每个任务一个请求?

vue.js - 如何在 Vue 中使用 VeeValidate 时修复 "TypeError: plugin is undefined"错误

javascript - 通过捕捉移动拇指穿过轨迹

javascript - 无法为 <md-app-content> vue 设置样式

vue.js - 如何通过在 Vue 中单击以编程方式触发可拖动的 "move"事件?

css - 如何覆盖 Vue 组件中的作用域样式?

javascript - 在 Vue 中使用 Axios 将查询参数传递给 URL 的最佳方式?

php - 拉维尔护照 404

vue.js - NUXT 中的 SSR vuex store