I'm trying to make a huge table reusable throughout my project with specified cells.
I want to pass my different cell components into my table component.
But I can't figure out how to pass a named slot through more than one child.
I'm using Vue v3.2.45

I'm expected to be able to use the slotProps of my named slot cell in App.vue
Like I did with id with the named slot test

I made a playground to make myself clear here

// App.vue
<script setup lang="ts">
import { ref } from 'vue'
import { ITable } from './types.ts'
import Table from './Table.vue'

const table = ref<ITable>({
  tree: [
    { data: [{ value: '0' }, { value: '1' }, { value: '2' }, { value: '3' }] },
    { data: [{ value: '0' }, { value: '1' }, { value: '2' }, { value: '3' }] },
  ],
})
</script>

<template>
  <Table :table="table">
    <template #cell="{ cell }">
      Hello {{ cell.value }}
    </template>
    <template #test="{ id }">
      <div class="row">Row above is {{ id }}</div>
    </template>
  </Table>
</template>
// Table.vue
<template>
  <div class="table">
    <template v-for="(row, rowI) in table.tree" :key="rowI">
      <Row :row="row" />
      <slot name="test" :id="rowI" />
    </template>
  </div>
</template>
// Row.vue
<template>
  <div class="row">
    <div class="cell" v-for="(cell, cellI) in row.data" :key="cellI">
      <slot name="cell" :cell="cell" />
    </div>
  </div>
</template>

推荐答案

您可以公开Table组件中的cell插槽,并在App.vue中使用它

<!-- Table.vue -->
<template>
  <div class="table">
    <template v-for="(row, rowI) in table.tree" :key="rowI">
      <Row :row="row">
        <template #cell="{ cell }">
          <slot name="cell" :cell="cell"></slot>
        </template>
      </Row>
      <slot name="test" :id="rowI" />
    </template>
  </div>
</template>

Vue.js相关问答推荐

Pinia+Vue 3状态react 性-StoreToRef的替代方案

Vue CLI: 如何在一个vtextfield更改时让另一个vtextfield的值也同时更改,但同时又可以覆盖它?

通过元素加中的正则表达式输入电话号码格式

如何使用 vuejs 在浏览器 url 中获取当前 product.id 的详细信息

Vuetify 扩展面板,面板标题左侧带有图标

在重复表行中

在 Vue 中启用(控制台)记录路由事件

确保在渲染组件之前加载 Vuex 状态

如何修复跨域读取阻塞 (CORB) 阻止了 MIME 类型应用程序/json 的跨域响应问题?

如何在 vuejs 中异步加载图像 src url?

使用 VueJS 在 v-for 中转换

vue-test-utils:无法覆盖属性 $route,这通常是由于插件将该属性添加为只读值

'vue' 未被识别为内部或外部命令

我应该将所有数据存储在 vuex 状态

Vue.js 单向绑定表单

在vue中单击时如何模糊元素

如何在 vue.js 中通过单击事件发出值

VS Code - 在 .vue 文件中启用 autoClosingTags 选项

如何在 Vue 数据对象中运行函数?

使用 vuex-persistedstate 仅使一个模块持久化