简体   繁体   English

(Vue) 在插槽内容中读取父级的道具

[英](Vue) Reading parent's props in slot content

I have a table component that takes a 'variant' prop which simply dictates some css styles to apply.我有一个表格组件,它采用“变体”道具,它简单地规定了一些要应用的 css 样式。 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.表格行只是填充 BaseTable.vue 中的默认<slot></slot> 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.我很想将变体属性传递给在该插槽中呈现的项目,这样我只需更改 BaseTable 上的变体即可在所有子组件上更改它。 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!提前致谢!

If I understand correctly, the following is probably currently the best option:如果我理解正确,以下可能是目前最好的选择:

<BaseTable :variant="'with_background'" v-slot="{ variant }" :columns="[{ name: 'Datum' }, { name: 'Bedrag', alignment: 'right' }, { name: 'Verkoper' }, { name: 'Status' }]">
  <BaseTableRow v-for="(pastOrder, index) in pastOrders" :key="index" :variant="variant">
    <BaseTableCell :variant="variant">{{ pastOrder.date }}</BaseTableCell>
    <BaseTableCell :variant="variant" :alignment="'right'">€{{ pastOrder.amount }}</BaseTableCell>
    <BaseTableCell :variant="variant">{{ pastOrder.supplier }}</BaseTableCell>
    <BaseTableCell :variant="variant">
      <BaseBadge :variant="pastOrder.status.variant">{{ pastOrder.status.text }}</BaseBadge>
    </BaseTableCell>
  </BaseTableRow>
</BaseTable>

And then in the underlying components, I use <slot :variant="variant"></slot> to pass it onto the rows and cells.然后在底层组件中,我使用<slot :variant="variant"></slot>将其传递给行和单元格。 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..我仍然想知道是否可以在每次使用此表时在父组件中的每个单元格上都指定 :variant="variant" 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM