1 回答

TA貢獻(xiàn)1744條經(jīng)驗 獲得超4個贊
您的問題是自定義tr組件。Table 只需要tbody, thead, tr, td,th元素,所以其他元素而不是預(yù)期的元素不屬于 table。您必須將整個表格放在組件中。
這個問題與任何虛擬節(jié)點框架(如 Angular)有關(guān),而不僅僅是 vue。
例子:
new Vue({
data: {
rows:["row1", "row2"]
},
components: {
myrows: {
props: ['row'],
template:'<tr><td>{{row}}</td></tr>',
},
mytable: {
props: ['rows'],
template:'<table><tr><td>TEST</td></tr><tr v-for="row in rows"><td>{{row}}</td></tr></table>',
}
}
}).$mount('div')
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div>
<table style='border:2px solid blue'><tr><td>TEST</td></tr><myrows v-for='row in rows' :row='row'></myrows></table>
<mytable style='border:2px solid red' :rows='rows'></mytable>
</div>
您必須在組件中提供整個表格。您的代碼必須如下所示:
new Vue({
data: {
creatures: [
{name:"Bat", hit_points: 200, armor_class: 5},
{name:"Org", hit_points: 250, armor_class: 25}
]
},
components: {
creatures: {
props: ['data'],
template: `<table>
<thead>
<tr>
<th>Initiative</th>
<th>Name</th>
<th>Hit Points</th>
<th>Armor Class</th>
</tr>
</thead>
<tbody>
<tr v-for='combatant in data'>
<td>?</td>
<td>{{ combatant.name }}</td>
<td>{{ combatant.hit_points }}</td>
<td>{{ combatant.armor_class }}</td>
</tr>
</tbody>
</table>`
}
}
}).$mount('div')
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div id="initiative">
<creatures :data="creatures"></creatures>
</div>
添加回答
舉報