[英]Rendering nested list with v-for and vue components
I am trying to render nested lists with vue components.我正在尝试使用 vue 组件呈现嵌套列表。 My approch so far is to create two compontens: One component to render the 'Blog' posts and one component to render the comments.
到目前为止,我的方法是创建两个组件:一个组件用于呈现“博客”帖子,一个组件用于呈现评论。 The 'Blogs' are rendering just fine, but the whole 'comments' are missing.
“博客”渲染得很好,但整个“评论”都不见了。 I get no errors other than a warning, that I should the lists should have explicit keys.
除了警告之外,我没有收到任何错误,我应该列表应该有明确的键。 Can someone explain why the 'comments' are not rendering?
有人可以解释为什么“评论”没有呈现吗?
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>
Have a look below.看看下面。 To add unique keys to your blogs and comments use:
要为您的博客和评论添加唯一键,请使用:
<my-blog v-for='(blog, i) in blogEntries'v-bind:blog='blog' :key="blog-${i}">
Where :key
is a shorthand for v-bind:key
.其中
:key
是v-bind:key
的简写。 So this can be use for comment
and blog
as wel.所以这也可以用于
comment
和blog
。 In order to have html of a comment in blog you need to make a blog aware of the comment position by adding a slot
.为了在博客中拥有 html 的评论,您需要通过添加
slot
让博客知道评论 position 。 You can also make your components more readable by using template ${literals}
您还可以使用
template ${literals}
使您的组件更具可读性
Also note that you have invalid HTML with <td>{{comment.content}}</div>
.另请注意,您使用
<td>{{comment.content}}</div>
的 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.