How to evaluate Vue.js component props in the data property?

后端 未结 1 820
执念已碎
执念已碎 2021-01-01 18:10

I have 2 components: Post and Comments.

Inside Post component, there is Comments component that has 3 props: postId, num

相关标签:
1条回答
  • 2021-01-01 18:37

    It looks like the comments prop does not have a value at the time of the component's creation (which is the only time allComments will be set).

    You can either:

    1. Defer the creation of the component until the comments prop is ready by using v-if like this:
    <comments v-if="comments" :comments="comments"></comments>
    
    1. Watch for changes to the comments prop and set allComments to the new value (in addition to initializing allComments in the data function):
    watch: {
      comments(value) {
        this.allComments = value;
      }
    }
    
    0 讨论(0)
提交回复
热议问题