0

我创建了一个显示各个页面的表格数据的组件。该组件在内部使用b-table。现在对于几个页面,我想自定义一些列的呈现,并且引导表允许使用具有特殊语法的范围字段槽:

    <template #cell(field)="data">
            {{ data.item.value }}
    </template>

其中字段 - 列名,来自我的列数组,以及 data.item - 要呈现的单元格项目。

问题是我对不同的页面有不同的字段,所以这个自定义应该来自父组件,并且这些模板应该是动态创建的。

以下是我尝试解决的方法:

通过属性传递给 MyTableComponent 一个具有可自定义字段和唯一插槽名称的数组在 MyTableComponent 中动态创建自定义模板,并在内部动态创建命名插槽

从父传递槽数据到命名槽

我的表组件:

    <b-table>
        <template v-for="slot in myslots" v-bind="cellAttributes(slot)">
            <slot :name="slot.name"></slot>
        </template>
    </b-table>

    <script>
        ...
        computed: {
            cellAttributes(slot) {
                return ['#cell(' + slot.field + ')="data"'];
            }
        }
        ...
    </script>

家长:

    <MyTableComponent :myslots="myslots" :items="items" :fields="fields">
        <template slot="customSlot1">
                Hello1
        </template>
        <template slot="customSlot1">
                Hello2
        </template>
    </MyTableComponent>

<script>
    ...
    items: [...my items...],
    fields: [...my columns...],
    myslots: [
        { field: "field1", name: "customSlot1" },
        { field: "field2", name: "customSlot2" }
    ]
    ...
</script>

不幸的是,b-table组件只是忽略了我的自定义插槽,就像没有提供它们一样。如果我直接在 MyTableComponent 中指定它,它会起作用:

    <b-table>
          <template #cell(field1)="data">
            {{ data.item.value }}
          </template>
    </b-table>

但我需要它通过组件属性动态完成。请帮忙。

4

1 回答 1

4

您可以使用Vue 2 的动态插槽名称功能将所有(或部分)插槽从父级传递到<b-table>内部子级,如下所示:

孩子:

<b-table>
  <template v-for="(_, slotName) of $scopedSlots" v-slot:[slotName]="scope">
    <slot :name="slotName" v-bind="scope"/>
  </template>
</b-table>

$scopedSlots包含传递给您的组件的所有插槽。

现在这将起作用:

    <MyTableComponent :items="items" :fields="fields">
          <template #cell(field1)="data">
            {{ data.item.value }}
          </template>
    </ MyTableComponent>

更新 2 - Vue 3

要使上述代码在 Vue 3 中工作,只需按照迁移指南的建议$scopedSlots替换$slots

更新 1

您可以根据需要进行过滤$scopedSlots(有一些特定于您的包装器组件的插槽,您不想传递给它<b-table>)通过创建computed

我在原始答案中提到了这种可能性,但它有点问题,所以值得更好的解释......

  1. 作用域插槽作为函数传递给组件(调用时生成 VNode)。目标组件只执行她知道的(按名称)并忽略其余部分。因此,假设您的包装器内部有b-table(或v-data-table用于 Vuetify)和其他一些组件,比如说分页。您可以在它们内部使用上面的代码,将所有插槽传递给每个插槽。除非存在一些命名冲突(两个组件使用相同的插槽名称),否则它将正常工作并且不会产生任何额外成本(所有插槽函数在传递给您的包装器组件时已经编译/创建)。目标组件将仅使用(执行)它通过名称知道的插槽。

  2. 如果可能存在命名冲突,可以通过使用一些命名约定来解决,例如为插槽名称添加前缀,b-tabletable--在内部进行过滤,但请注意$scopedSlots对象确实包含一些必须复制的 Vue 内部属性!( $stable,对于 Vue 2 - 请参阅$key代码)。所以下面的过滤代码即使它非常好并且不会抛出任何错误也不起作用(不会识别和使用插槽)$hasNormalb-table

<b-table>
  <template v-for="(_, slotName) of tableSlots" v-slot:[slotName]="scope">
    <slot :name="slotName" v-bind="scope"/>
  </template>
</b-table>
computed: {
    tableSlots() {
      const prefix = "table--";
      const raw = this.$scopedSlots;
      const filtered = Object.keys(raw)
        .filter((key) => key.startsWith(prefix))
        .reduce(
          (obj, key) => ({
            ...obj,
            [key.replace(prefix, "")]: raw[key],
          }),
          {}
        );
      return filtered;
    },
  },

这段代码可以通过包含上面提到的属性来修复,但这对我的口味来说过于依赖 Vue 内部实现,我不推荐它。如果可能,请坚持方案1 ...

于 2020-11-01T21:57:32.747 回答