Vue.js 将动态插槽从父级向下传递到子级向下传递到孙子级
有人知道如何将动态槽从父代传递到孙代吗 我知道如何使用静态命名插槽,但不知道如何使用动态命名插槽 例如,假设插槽模板在父节点中为“name”,而孙子节点具有基于动态列的插槽 如何在子对象中添加模板以传递此信息 下面是我的代码示例:Vue.js 将动态插槽从父级向下传递到子级向下传递到孙子级,vue.js,vue-component,Vue.js,Vue Component,有人知道如何将动态槽从父代传递到孙代吗 我知道如何使用静态命名插槽,但不知道如何使用动态命名插槽 例如,假设插槽模板在父节点中为“name”,而孙子节点具有基于动态列的插槽 如何在子对象中添加模板以传递此信息 下面是我的代码示例: // GrandChild.vue <template> <table> <tbody> <template v-for="(item, itemIndex) in items
// GrandChild.vue
<template>
<table>
<tbody>
<template v-for="(item, itemIndex) in items">
<tr :key="itemIndex">
<template v-for="(col, colIndex) in columns">
<slot
v-if="$scopedSlots[col]"
:name="col"
:item="item"
/>
<td
v-else
:key="colIndex"
>
{{ String(item[col]) }}
</td>
</template>
</tr>
</template>
</body>
</table>
</template>
<script>
export default {
props: ['items', 'columns']
}
</script>
感谢您的帮助。我已经解决了。我会回答我自己的问题,以防将来它能帮助别人 为了连接父代和孙代,我在case Child.vue的中间组件中添加了以下内容
<template v-for="field in Object.keys($scopedSlots)" v-slot:[field]="{item}">
<slot :name="field" :item="item"/>
</template>
完整代码:
// Child.vue
<template>
<grand-child :items="items" :columns="columns">
<template v-for="field in Object.keys($scopedSlots)" v-slot:[field]="{item}">
<slot :name="field" :item="item"/>
</template>
</grand-child>
</template>
<script>
export default {
props: ['items', 'columns']
}
</script>
<template v-for="field in Object.keys($scopedSlots)" v-slot:[field]="{item}">
<slot :name="field" :item="item"/>
</template>
// Child.vue
<template>
<grand-child :items="items" :columns="columns">
<template v-for="field in Object.keys($scopedSlots)" v-slot:[field]="{item}">
<slot :name="field" :item="item"/>
</template>
</grand-child>
</template>
<script>
export default {
props: ['items', 'columns']
}
</script>