Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 将动态插槽从父级向下传递到子级向下传递到孙子级_Vue.js_Vue Component - Fatal编程技术网

Vue.js 将动态插槽从父级向下传递到子级向下传递到孙子级

Vue.js 将动态插槽从父级向下传递到子级向下传递到孙子级,vue.js,vue-component,Vue.js,Vue Component,有人知道如何将动态槽从父代传递到孙代吗 我知道如何使用静态命名插槽,但不知道如何使用动态命名插槽 例如,假设插槽模板在父节点中为“name”,而孙子节点具有基于动态列的插槽 如何在子对象中添加模板以传递此信息 下面是我的代码示例: // GrandChild.vue <template> <table> <tbody> <template v-for="(item, itemIndex) in items

有人知道如何将动态槽从父代传递到孙代吗

我知道如何使用静态命名插槽,但不知道如何使用动态命名插槽

例如,假设插槽模板在父节点中为“name”,而孙子节点具有基于动态列的插槽

如何在子对象中添加模板以传递此信息

下面是我的代码示例:

// 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>