1. 程式人生 > >vue.js為什麼在component的template的root標籤中不能使用v-for?

vue.js為什麼在component的template的root標籤中不能使用v-for?

具體問題的描述:

<div v-for="article in articles" style="margin:13px">
    <div class="article-image">
        <a>
        <img :src="article.image_path"/>
        </a>
    </div>
    <div class="article-title">
        <h4>{{article.title}}</h4>
    </div
>
<div class="article-content"> <p v-html="article.content"></p> </div> </div>
Vue.component('article_item', {
    template: "#article-template",
    replace: true,
    props: {
        articles: Array
    }
});

前端報錯:

Cannot use v-for on stateful component
root element because it renders multiple elements.

原因:

v-for不能用於根元素(root element)。因為v-for是個迴圈,它返回更多的元素。導致無法渲染

解決辦法:

新增根節點

<div id="test">
    <div  v-for="objects in object"> {{ object.value }}</div>
</div>