vue.js – [Vue warn]:渲染错误:“TypeError:无法读取未定义的属性’name’”

我收到了错误

TypeError: Cannot read property ‘name’ of undefined”

如果我深入到物体中

Timeline.vue

<template>
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="card card-default">
                <div class="card-header">Timeline</div>

                <div class="card-body">
                    <post v-for="post in posts" :key="post.id"></post>
                </div>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import Post from './Post.vue'

export default {
    data () {
        return {
            posts: []
        }
    },
    components: {
        Post
    },
    mounted() {
        this.$http.get('/posts').then((response) => {
            console.log(response.body)
            this.posts =response.body
        })
    }
}
</script>

post.vue

<template>
<div class="media">

    <div class="media-left">

    </div>
    <div class="media-body">
        <strong>test</strong>
        <strong>{{ post.user.name }} </strong> 
    </div>
</div>
</template>

<script>
export default {
   props: [
        'post'
    ]
}
</script>

那为什么我得到错误?
我想{{post.user.name}}有些问题.

最佳答案 你需要像这样在模板中传递post prop.否则它在子组件(post)中是未定义的.

<div class="card-body">
    <post 
        v-for="post in posts" 
        :key="post.id"
        :post="post">
    </post>
</div>
点赞