组件模板中的Vujs迭代不起作用

时间:2019-05-10 00:16:22

标签: javascript vue.js vuejs2

为什么不能按如下所示在组件模板内部进行迭代?

<!DOCTYPE html>
<html>
<head>
  <title>My first Vue app</title>
  <script src="https://cdn.jsdelivr.net/npm/vue"></script>
  <style>
  </style>
</head>
<body>
    <div id="blog-post-demo">
      <blog-post :posts="posts"></blog-post>
    </div>
  <script>
    
Vue.component('blog-post', {
  props: ['posts'],
  template: `
    <div class="blog-post" v-for="post in posts">
      <h3> {{ post.title }}</h3>
      <button>Enlarge text</button>
      <div v-html="post.content"></div>
    </div>`,
})
new Vue({
  el   : '#blog-post-demo',
  data : {
    posts : [
      {id: 1, title : 'My Journey to Africa',    content : 'I am the post'},
      {id: 2, title : 'My Journey to America',   content : 'I am the post'},
      {id: 3, title : 'My Journey to Antartica', content : 'I am the post'},
      {id: 4, title : 'My Journey to Asia',      content : 'I am the post'},
    ],
  }
})
  </script>
</body>
</html>

下面的一个可行,但是我对为什么上面的一个不可行感到困惑。任何解释都会有所帮助!

<!DOCTYPE html>
<html>
<head>
  <title>My first Vue app</title>
  <script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
    <div id="blog-post-demo">
      <blog-post v-for="post in posts" :post="post"></blog-post>
    </div>
  <script>
    
Vue.component('blog-post', {
  props: ['post'],
  template: `
    <div class="blog-post">
      <h3> {{ post.title }}</h3>
      <button>Enlarge text</button>
      <div v-html="post.content"></div>
    </div>`,
})
new Vue({
  el   : '#blog-post-demo',
  data : {
    posts : [
      {id: 1, title : 'My Journey to Africa',    content : 'I am the post'},
      {id: 2, title : 'My Journey to America',   content : 'I am the post'},
      {id: 3, title : 'My Journey to Antartica', content : 'I am the post'},
      {id: 4, title : 'My Journey to Asia',      content : 'I am the post'},
    ],
  }
})
  </script>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

简单:

“无法在有状态组件根元素上使用v-for,因为它会呈现多个元素。”

模板只能在根元素上具有,您可以根据需要将组件包装在DIV标记中,但是在这种简单情况下,我实际上认为您的第二个示例更优雅,并且符合该组件的单一责任原则。 / p>

更多信息可以在VueJS官方文档中找到: https://vuejs.org/v2/guide/components.html#A-Single-Root-Element

答案 1 :(得分:1)

问题是您不能将重复的元素作为模板根。

如果使用Vue.js的开发版本,则会看到...

  

[Vue警告]:编译模板时出错:

     

无法在有状态组件根元素上使用v-for,因为它会呈现多个元素。

将组件的模板更改为

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

演示

Vue.component('blog-post', {
  props: ['posts'],
  template: `<div>
    <div class="blog-post" v-for="post in posts" :key="post.id">
      <h3> {{ post.title }}</h3>
      <button>Enlarge text</button>
      <div v-html="post.content"></div>
    </div>
  </div>`,
})
new Vue({
  el   : '#blog-post-demo',
  data : {
    posts : [
      {id: 1, title : 'My Journey to Africa',    content : 'I am the post'},
      {id: 2, title : 'My Journey to America',   content : 'I am the post'},
      {id: 3, title : 'My Journey to Antartica', content : 'I am the post'},
      {id: 4, title : 'My Journey to Asia',      content : 'I am the post'},
    ],
  }
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"></script>
<div id="blog-post-demo">
  <blog-post :posts="posts"></blog-post>
</div>