【发布时间】:2018-10-29 22:15:03
【问题描述】:
我有一些相关的和相邻的表列,我想将它们分组到同一个组件中。但是 Vue.js 模板系统有这个限制,只有一个标签可以直接在<template> 中。通常这个无意义的包装是<div>。但是我可以在表格中使用什么?
我可以为此滥用<span> 之类的标签,但后果是不可接受的。同一列中的单元格大小不同,表格边框不折叠。有没有办法让包装标签在理想情况下根本不会出现在 HTML 中,或者至少像 <div> 一样中性?
表格行:
<template>
<tr v-for="thing in things">
<td>{{thing.name}}</td>
<size-component :size="thing.size"></size-component>
<time-component :time="thing.time"></time-component>
</tr>
</template>
列大小:
<template>
<wrap>
<td>{{size.x}}</td>
<td>{{size.y}}</td>
<td>{{size.z}}</td>
</wrap>
</template>
时间列:
<template>
<wrap>
<td>{{time.h}}</td>
<td>{{time.m}}</td>
<td>{{time.s}}</td>
</wrap>
</template>
编辑:
对我来说,这归结为问题,在<tr> 中没有标签可以将<td>s 分组(就像<tr>s 可以用多个<tbody> 标签分组在<table> 中)。比较Is there a tag for grouping "td" or "th" tags?
语义上<colgroup> 是为此目的而设计的,但这并没有帮助。
对我来说,使用 vue-fragment 原来是正确的解决方案:
<template>
<fragment>
<td>{{size.x}}</td>
<td>{{size.y}}</td>
<td>{{size.z}}</td>
<td>{{volume}}</td>
</fragment>
</template>
<script>
import { Fragment } from 'vue-fragment';
export default {
computed: {
volume() { return this.size.x * this.size.y * this.size.z },
},
components: { Fragment },
props: ['size']
}
</script>
【问题讨论】:
标签: html templates vue.js html-table