admin管理员组文章数量:1426642
I am seeking an explanation of why I need the main wrapping div in this template in Vue. Without it, if the main element is the div with v-for
- then listing of items stops working.
<script type="text/x-template" id="devlistitem">
<div><div class="item" v-for="item in cd_items">
<div v-if="!item.u" class="heade">{{ item.p }}</div>
<div v-if="item.desc" class="desc">{{ item.desc }}</div>
</div></div>
</script>
<script>// ponent
Vueponent('devlistitems', {
props: ['dataitems']
, puted: { cd_items: function () {
var x = this.dataitems; return APP_DATA[x]; }
}
, template:'#devlistitem'}
);</script>
Data are defined right in source, no ajax, and in main app I simply use a ponent with a property telling which data to use:
<devlistitems dataitems="json_items" />
Meaning to use APP_DATA['json_items']
as source data. Everything works ok, but removing the outer div makes it stop working.
I am seeking an explanation of why I need the main wrapping div in this template in Vue. Without it, if the main element is the div with v-for
- then listing of items stops working.
<script type="text/x-template" id="devlistitem">
<div><div class="item" v-for="item in cd_items">
<div v-if="!item.u" class="heade">{{ item.p }}</div>
<div v-if="item.desc" class="desc">{{ item.desc }}</div>
</div></div>
</script>
<script>// ponent
Vue.ponent('devlistitems', {
props: ['dataitems']
, puted: { cd_items: function () {
var x = this.dataitems; return APP_DATA[x]; }
}
, template:'#devlistitem'}
);</script>
Data are defined right in source, no ajax, and in main app I simply use a ponent with a property telling which data to use:
<devlistitems dataitems="json_items" />
Meaning to use APP_DATA['json_items']
as source data. Everything works ok, but removing the outer div makes it stop working.
- and second - the root element does not let me replace it with <template> instead of <div> - sstops working again – Peminator Commented Nov 12, 2018 at 9:30
3 Answers
Reset to default 4Template can have only one root element.V-for returns multiple root elements, so it's necessary to enclose it inside an element.
Hope this helps!!
According to https://v2.vuejs/v2/guide/ponents.html#A-Single-Root-Element
Every ponent must have a single root element
As a sample from the site, this will not work
<h3>{{ title }}</h3>
<div v-html="content"></div>
To fix you can do some thing like:
<div class="blog-post">
<h3>{{ title }}</h3>
<div v-html="content"></div>
</div>
You need to re-design this ponent.
The ponent manages the for loop internally
. You can have a dumb ponent that only shows one item
. The parent loops and shows item
.
Again have a look atblog-post
sample at the https://v2.vuejs/v2/guide/ponents.html#A-Single-Root-Element
Vue can only have A Single Root Element Error:
<div><p>Text</p></div>
<div><p>Text2</p></div>
Correct:
<div>
<div><p>Text</p></div>
<div><p>Text2</p></div>
</div>
https://v2.vuejs/v2/guide/ponents.html#A-Single-Root-Element
本文标签: javascriptVuejswhy root element can not have the vfor and needs div wrapperStack Overflow
版权声明:本文标题:javascript - Vue.js - why root element can not have the v-for and needs div wrapper - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745465744a2659526.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论