Vue.js将插槽传递给包装的Bootstrap-Vue Table组件

时间:2019-10-17 14:49:38

标签: javascript vue.js vuejs2 bootstrap-vue

我正在尝试为bootstrap-vue Table组件创建包装。该组件使用插槽来定义单元格模板,如下所示:

<b-table :items="itemsProvider" v-bind="options">
    <template v-slot:cell(id)="data">
        ///...here goes the template for the cell's of itens key "id"
    </template>
</b-table>

所以,我正在创建的包装器是这样的:

    <div>
        <b-table :items="itemsProvider" v-bind="options" >
            <slot></slot>
        </b-table>
        <b-pagination
                v-model="currentPage"
                :total-rows="rows"
                :per-page="perPage"
                 />
    </div>

我想这样调用此组件:

<TableAjax :options="options">
    <template v-slot:cell(id)="data">
        ///...here goes the template for the cell's of itens key "id"                    
    </template>
</TableAjax>

但是,由于b表组件所需的插槽已命名,因此我很难从包装器中传递它。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

将插槽传递给子组件可以像这样完成:

<template>
  <div>
    <b-table :items="itemsProvider" v-bind="options" >
      <template v-slot:cell(id)="data">
         <slot name="cell(id)" v-bind="data"></slot>
      </template>
    </b-table>
    <b-pagination
      v-model="currentPage"
      :total-rows="rows"
      :per-page="perPage"
    />
  </div>
</template>

但是由于您可能不知道插槽名称,因此您需要执行以下操作:

<template>
  <div>
    <b-table :items="itemsProvider" v-bind="options" >
      <template v-for="slotName in Object.keys($scopedSlots)" v-slot:[slotName]="slotScope">
        <slot :name="slotName" v-bind="slotScope"></slot>
      </template>
    </b-table>
    <b-pagination
      v-model="currentPage"
      :total-rows="rows"
      :per-page="perPage"
    />
  </div>
</template>