As a newcomer to Vue, I am facing a challenge in implementing a wrapper component similar to React's 'Wrapper' component. Specifically, I want to create a reusable datagrid
component using a 3rd-party some-table
component and a pagination
component. The goal is for the datagrid
component to provide the necessary data/props/events for both child components and manage communication between them. In React, achieving this would be as simple as:
// inside <Datagrid />
<Table {...someProps} />
<Pagination {...otherProps} />
However, in Vue, it seems that passing down props
to children components is not as straightforward:
// inside Datagrid.vue
<some-table v-bind="$props"></some-table>
I have considered using slots
, but I'm unsure if they can fully address my requirements. The wrapper component I envision should gather all the necessary props/events/slots
required by its children (potentially 3rd-party components) and pass them down effectively. Additionally, it should handle tasks like data exchange between its children components. While datagrid
could serve as a slots wrapper, the issue arises when both table
and pagination
need access to the same data
prop, which I believe should be managed by datagrid
. How can I efficiently share this data
with the slots?
// Datagrid.vue
<template>
<div>
<slot name="table"></slot>
<slot name="pagination"></slot>
</div>
</template>
<script>
export default {
name: 'Datagrid',
data() {
return {
data: 'How to share it with table and pagination',
}
},
}
</script>
I have explored some possible solutions:
- Using Render Functions, though I believe this may overcomplicate the situation
- Instead of creating a 'Container' component, utilizing mixins. However, does this mean I have to include
every time I need a datagrid?<pagination :total="total" :other-props="are-same-for-all-datagrids"></pagination>
Are there any examples or best practices that address such scenarios in Vue? Your insights are greatly appreciated!