(vue)在插槽内容中读取父母的道具
我有一个表格组件,该组件采用“变体”道具,它只是决定了某些CSS样式要应用。我使用如下:
<BaseTable :variant="'with_background'" :columns="[{ name: 'Datum' }, { name: 'Bedrag', alignment: 'right' }, { name: 'Verkoper' }, { name: 'Status' }]">
<BaseTableRow v-for="(pastOrder, index) in pastOrders" :key="index" :variant="'with_background'">
<BaseTableCell :variant="'with_background'">{{ pastOrder.date }}</BaseTableCell>
<BaseTableCell :variant="'with_background'" :alignment="'right'">€{{ pastOrder.amount }}</BaseTableCell>
<BaseTableCell :variant="'with_background'">{{ pastOrder.supplier }}</BaseTableCell>
<BaseTableCell :variant="'with_background'">
<BaseBadge :variant="pastOrder.status.variant">{{ pastOrder.status.text }}</BaseBadge>
</BaseTableCell>
</BaseTableRow>
</BaseTable>
如您所见,表行和单元格还需要变体道具。表行只需填充默认的&lt; slot&gt;&lt;/slot&gt;
在basetable.vue中。我很想将变体属性传递给该插槽中呈现的项目,这样我只需要更改可用于所有子组件上的基本上的变体即可更改它。这可能吗?现有问题似乎讨论了父组件中插槽属性的使用情况,但是每当我创建表时,我仍然必须将其添加到每个单元格中。
提前致谢!
I have a table component that takes a 'variant' prop which simply dictates some css styles to apply. I use it as follows:
<BaseTable :variant="'with_background'" :columns="[{ name: 'Datum' }, { name: 'Bedrag', alignment: 'right' }, { name: 'Verkoper' }, { name: 'Status' }]">
<BaseTableRow v-for="(pastOrder, index) in pastOrders" :key="index" :variant="'with_background'">
<BaseTableCell :variant="'with_background'">{{ pastOrder.date }}</BaseTableCell>
<BaseTableCell :variant="'with_background'" :alignment="'right'">€{{ pastOrder.amount }}</BaseTableCell>
<BaseTableCell :variant="'with_background'">{{ pastOrder.supplier }}</BaseTableCell>
<BaseTableCell :variant="'with_background'">
<BaseBadge :variant="pastOrder.status.variant">{{ pastOrder.status.text }}</BaseBadge>
</BaseTableCell>
</BaseTableRow>
</BaseTable>
As you can see, the variant prop is also required by the table rows and cells. The table rows simply fill a default <slot></slot>
in BaseTable.vue. I would love to pass the variant property to the items that are rendered in that slot, so that I would only have to change the variant on BaseTable to change it on all subcomponents. Is this possible? Existing questions seem to discuss the usage of slot properties in the parent component, but then I would still have to add it to every cell whenever I create a table.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解,以下内容可能是当前最好的选择:
然后在基础组件中,我使用
&lt; slot:variant =“ variant”&gt;&lt;&lt;/slot&gt;
将其传递到行和细胞。我仍然想知道是否可以在没有指定的情况下进行:variant =“变体”,每次使用此表时,父组件中的每个单元格中的每个单元格。If I understand correctly, the following is probably currently the best option:
And then in the underlying components, I use
<slot :variant="variant"></slot>
to pass it onto the rows and cells. I do still wonder if that is possible without specifying :variant="variant" on every cell in the parent component every time I use this table..