我是Vue的新手,無法找到一種方法來實現類似Vue.js的React-like「Wrapper」組件,例如,一個可重用的datagrid
組件使用第三方some-table
組件和pagination
組件。直觀上,datagrid
將提供兩個組件需要的數據/道具/事件並控制它們之間的通信。在陣營,可以這樣做簡單,如像如何在Vue.js中創建'容器'組件
// inside <Datagrid />
<Table {...someProps} />
<Pagination {...otherProps} />
隨着Vue公司,這似乎是類似下面只能通過props
到子組件
// inside Datagrid.vue
<some-table v-bind="$props"></some-table>
我不知道如果slots
可能會有所幫助。我一直在努力的這個包裝組件需要它的孩子需要的所有props/events/slots
,並將它們傳遞給它們,以便我可以利用它的所有功能,它是兒童(可能是一些第三方組件)提供的。此外,它也可能需要爲其子女之間的數據交換負責。雖然datagrid
可能是一個插槽包裝,但如果table
和pagination
需要相同的data
支持,我認爲應該位於datagrid
。如何將這個data
傳遞給插槽?
// 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>
一些解決方案,我可以計算出:
- Render Functions,但我不認爲同謀需要在這種情況下
- 而是創建一個「容器」組件,只需打開到mixins,但這是否意味着我每次需要輸入數據網格時都必須輸入
<pagination :total="total" :other-props="are-same-for-all-datagrids"></pagination>
?
在Vue中處理這種情況的任何示例?提前致謝!
插槽是你想要什麼。 –