简体   繁体   English

Vue3使用道具将插槽从父级传递给孙级

[英]Vue3 passing a slot from parent to grandchild with props

I am trying to pass a custom component as slot from a parent component to grandchild with props that are calculated in child.我正在尝试使用在子组件中计算的道具将自定义组件作为插槽从父组件传递给孙子组件。

The grandchild component has a v-for loop in it, and the component that is eventually passed to the it gets its props from the grandchild.孙子组件中有一个v-for循环,最终传递给它的组件从孙子组件中获取道具。 These props are optional in the custom component's template and only the custom component is passed to the parent without any props are passed to the parent.这些道具在自定义组件的模板中是可选的,只有自定义组件被传递给父组件,而没有任何道具被传递给父组件。

// Parent, no props are passed to RowComponent
<Child
   :array="this.data">
  <RowComponent/>
</Child>

// Child, no props are passed to the slot
  <template #detailed>
    <GrandChild :data="this.data">
      <slot></slot>
    </GrandChild>
  </template>

// Grandchild, passes props to the slot
<template>
  <div
    v-for="(item, index) in data"
    :key="index"
  >
    <slot :data="item" :idx="index"> </slot>
  </div>
</template>

I need RowComponent to capture the props passed by grandchild but, apparently, whatever is passed by the parent takes effect and passing props in grandchild doesn't work.我需要RowComponent来捕获孙子传递的道具,但是显然,父母传递的任何东西都会生效,并且在孙子中传递道具不起作用。

How can I pass pass these props to my custom component at grandchild level (they are not known at the parent level)?如何将这些道具传递给孙级的自定义组件(它们在父级不知道)?

EDIT: updated component names编辑:更新的组件名称

Your code doesn't really make sense, If you think about it.如果您考虑一下,您的代码实际上没有意义。 You're not really passing anything to RowComponent .您并没有真正将任何东西传递给RowComponent

I believe this is what you're looking for我相信这就是你要找的

https://vuejs.org/guide/components/slots.html#scoped-slots https://vuejs.org/guide/components/slots.html#scoped-slots

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM