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 组件上所需的插槽已命名,因此我很难从包装器中传递它。
我怎样才能做到这一点?
可以像这样将插槽传递给子组件:
<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)
| 归档时间: |
|
| 查看次数: |
1499 次 |
| 最近记录: |