vue列表渲染问题

一个Vue列表渲染,代码如下,在网页上却没有渲染出来,有些不太明白,求指导

<html>  
  <head>
    <script src="http://cdn.bootcss.com/vue/2.1.10/vue.min.js"></script>
  </head>  
  <body> 
        <ul id="example-1">
            <examp></examp>
        </ul>
        <script type="text/javascript">
            Vue.component('examp',{
                template:'<li v-for="item in items">{{ item.message }}</li>',
              data: function(){
                  return {
                      items:[{message: 'Foo' },{message: 'Bar' }]
                  }
              }
            })
            new Vue({
                el: '#example-1'
            })
        </script>
    </body>
</html> 
阅读 2.3k
1 个回答

v-for后,你的组件就会多个li元素,也就产生了多个根节点,Vue2.0是不允许的,考虑如下修改:

<html>

<head>
    <script src="http://cdn.bootcss.com/vue/2.1.10/vue.min.js"></script>
</head>

<body>
    <div id="example-1">
        <ul is="examp"></ul>
    </div>
    <script type="text/javascript">
        Vue.component('examp', {
            template: '<ul><li v-for="item in items">{{item.message}}</li></ul>',
            data: function () {
                return {
                    items: [{
                        message: 'Foo'
                    }, {
                        message: 'Bar'
                    }]
                }
            }
        })
        new Vue({
            el: '#example-1'
        })
    </script>
</body>

</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题