【问题标题】:Rendering nested list with v-for and vue components使用 v-for 和 vue 组件渲染嵌套列表
【发布时间】:2021-08-19 01:14:05
【问题描述】:

我正在尝试使用 vue 组件呈现嵌套列表。到目前为止,我的方法是创建两个组件:一个组件用于呈现“博客”帖子,一个组件用于呈现 cmets。 “博客”渲染得很好,但整个“cmets”都不见了。除了警告之外,我没有收到任何错误,我应该列表应该有明确的键。有人可以解释为什么“cmets”没有渲染吗?

var myBlogMainBody = {
    props: ['blog'],
    template: '<div>'+
              '<h3>{{blog.title}}</h3>'+
              '<p>{{blog.content}}</p></div>'
}

var myCommentBody = {
    props: ['comment'],
    template: '<div><h4>Kommentare</h4>' +
              '{{comment.author}}' +
              '<td>{{comment.content}}</div>' /* + 
              '<table><tr><td>Autor</td><td><input v-model="blog.comment.author"></textarea></td></tr>' +
              '<tr><td>Kommentar</td><td><textarea v-model="blog.comment.content"></textarea></td></tr></table>' +
              '<button v-on:click="$emit(`commit-new-comment`,comment)">Kommentieren</button></div>' */
}

var vm = new Vue({
    el:'#app',
    data:{ blogEntries:[{
                title: 'Erster Blogeintrag',
                content: 'Hier steht mein Inhalt',
                comments:[{
                    author: 'Henning',
                    content: 'Scheiß blog'
                },{
                    author:'Maike',
                    content: 'Guter Blog!'
                }]
            },
            {
                title: 'Zweiter Blogeintrag',
                content: 'Hier steht mein Inhalt',
                comments:[{
                    author: 'Henning',
                    content: 'Scheiß blog'
                },{
                    author:'Maike',
                    content: 'Guter Blog!'
                },{
                    author:'Maike',
                    content: 'Guter Blog!'
                }]
            }],
            
        },
    methods:{
        addNewBlog: function(blog){
            let newBlog = {
                title: blog.title,
                content: blog.content
            }
            this.blogEntries.push(newBlog);
        }
    },
    components:{
        'my-blog': myBlogMainBody,
        'my-comment': myCommentBody
    }
});
      <script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.js"></script>
    <div id=app>
        <my-blog v-for='blog in blogEntries'v-bind:blog='blog'>
          <my-comment v-for='comment in blog.comments' v-bind:comment='comment'></my-comment>
        </my-blog>
        
        <create-blog-entry v-on:commit-new-blog='addNewBlog'></create-blog-entry>
    </div>
    <script type="text/javascript" src="myBlog.js"></script>

【问题讨论】:

  • 你应该给每个博客和评论一个唯一的:key。这可以通过使用&lt;my-blog v-for='(blog, i) in blogEntries'v-bind:blog='blog' :key="blog-${i}"&gt; 来完成。使用:keyv-bind:key 的简写。
  • &lt;td&gt;{{comment.content}}&lt;/div&gt; 也不是有效的 HTML。
  • @SuperDJ 是的&lt;td&gt; 只是剩下的,但它不会改变结果。为什么我需要钥匙?即使我插入那个键它也不起作用..
  • 我的回答应该能回答这个问题

标签: javascript html vue.js vue-component


【解决方案1】:

请看下面。要为您的博客和 cmets 添加唯一键,请使用:

<my-blog v-for='(blog, i) in blogEntries'v-bind:blog='blog' :key="blog-${i}">

其中:keyv-bind:key 的简写。所以这也可以用于commentblog。为了在博客中有评论的 html,您需要通过添加 slot 让博客知道评论位置。您还可以使用 template ${literals} 使您的组件更具可读性

另请注意,&lt;td&gt;{{comment.content}}&lt;/div&gt; 的 HTML 无效。

var myBlogMainBody = {
    props: ['blog'],
    template: `<div>
              <h3>{{blog.title}}</h3>
              <p>{{blog.content}}</p><slot></slot></div>`
}

var myCommentBody = {
    props: ['comment'],
    template: `<div><h4>Kommentare</h4>
              {{comment.author}}
              <div>{{comment.content}}</div></div>` /* 
              '<table><tr><td>Autor</td><td><input v-model="blog.comment.author"></textarea></td></tr>' +
              '<tr><td>Kommentar</td><td><textarea v-model="blog.comment.content"></textarea></td></tr></table>' +
              '<button v-on:click="$emit(`commit-new-comment`,comment)">Kommentieren</button></div>' */
}

var vm = new Vue({
    el:'#app',
    data:{ blogEntries:[{
                title: 'Erster Blogeintrag',
                content: 'Hier steht mein Inhalt',
                comments:[{
                    author: 'Henning',
                    content: 'Scheiß blog'
                },{
                    author:'Maike',
                    content: 'Guter Blog!'
                }]
            },
            {
                title: 'Zweiter Blogeintrag',
                content: 'Hier steht mein Inhalt',
                comments:[{
                    author: 'Henning',
                    content: 'Scheiß blog'
                },{
                    author:'Maike',
                    content: 'Guter Blog!'
                },{
                    author:'Maike',
                    content: 'Guter Blog!'
                }]
            }],
            
        },
    methods:{
        addNewBlog: function(blog){
            let newBlog = {
                title: blog.title,
                content: blog.content
            }
            this.blogEntries.push(newBlog);
        }
    },
    components:{
        'my-blog': myBlogMainBody,
        'my-comment': myCommentBody
    }
});
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.js"></script>
    <div id="app">
        <my-blog v-for="(blog, b) in blogEntries" :blog="blog" :key="`blog-${b}`">
          <my-comment v-for="(comment, c) in blog.comments" :comment="comment" :key="`comment-${c}`"/>
        </my-blog>
        
        <create-blog-entry v-on:commit-new-blog="addNewBlog"/>
    </div>
    <script type="text/javascript" src="myBlog.js"></script>

【讨论】:

    猜你喜欢
    • 2020-05-27
    • 2021-04-16
    • 1970-01-01
    • 2021-12-15
    • 2020-08-12
    • 2021-09-13
    • 1970-01-01
    • 2019-05-18
    • 2021-12-18
    相关资源
    最近更新 更多