Vue.js 将槽传递给包装好的 Bootstrap-Vue 表组件

ren*_*rof 4 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>
Run Code Online (Sandbox Code Playgroud)

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

    <div>
        <b-table :items="itemsProvider" v-bind="options" >
            <slot></slot>
        </b-table>
        <b-pagination
                v-model="currentPage"
                :total-rows="rows"
                :per-page="perPage"
                 />
    </div>
Run Code Online (Sandbox Code Playgroud)

我想像这样调用这个组件:

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

Run Code Online (Sandbox Code Playgroud)

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

我怎样才能做到这一点?

Tro*_*use 9

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

  • 你的例子不起作用,但给了我前进的道路!谢谢!我已将您的答案编辑为正确的解决方案! (4认同)