【发布时间】:2023-03-06 18:10:01
【问题描述】:
我正在使用 vue 2.5 和库 Bootstrap-vue。 我对这个库的表格组件很感兴趣:https://bootstrap-vue.js.org/docs/components/table
但是我想封装这个组件以使用我不想重复的自定义配置来制作我自己的组件。这样我就不必每次都管理分页和过滤,并且可以添加其他功能,例如数据导出。
所以我创建了一个表格助手组件(现在只处理分页)
<template>
<div>
<b-table striped hover responsive
:items="items" :fields="fields"
:current-page="currentPage" per-page="10">
<slot></slot>
</b-table>
<b-pagination :total-rows="items.length" per-page="10" v-model="currentPage"></b-pagination>
</div>
</template>
<script>
import bTable from 'bootstrap-vue/es/components/table/table'
import bPagination from 'bootstrap-vue/es/components/pagination/pagination'
export default {
name: "table-helper",
props: ['items', 'fields'],
data() {
return {
currentPage: 1,
}
},
components: {
'b-table': bTable,
'b-pagination': bPagination
}
}
</script>
我想像这样使用我的组件(使用 bootstrap-vue 插槽重新格式化列):
<table-helper :items="users" :fields="fields">
<template slot="fullName" slot-scope="data">
{{data.item.first_name}} {{data.item.last_name}}
</template>
</table-helper>
显然它不起作用(我得到了表格但没有格式化列),因为<template slot="fullName" slot-scope="data"> 指的是我的自定义组件而不是 b-table 组件。
所以我想知道一种封装库组件的方法,它使用像这样的插槽和插槽范围。
感谢您的帮助。
【问题讨论】:
标签: vuejs2