【问题标题】:Vue - how to pass down slots inside wrapper component?Vue - 如何在包装器组件中传递插槽?
【发布时间】:2018-11-26 06:14:44
【问题描述】:

所以我创建了一个简单的包装器组件,其模板如下:

<wrapper>
   <b-table v-bind="$attrs" v-on="$listeners"></b-table>
</wrapper>

使用$attrs$listeners 传递道具和事件。
工作正常,但是包装器如何将 &lt;b-table&gt; 命名的插槽代理给孩子?

【问题讨论】:

标签: vue.js components vue-component wrapper bootstrap-vue


【解决方案1】:

Vue 3

与下面的 Vue 2.6 示例相同,除了:

  • $listeners 已合并到 $attrs 中,因此不再需要 v-on="$listeners"。请参阅migration guide
  • $scopedSlots 现在只是 $slots。见migration guide

Vue 2.6(v-slot 语法)

所有普通槽都会被添加到作用域槽中,所以你只需要这样做:

<wrapper>
  <b-table v-bind="$attrs" v-on="$listeners">
    <template v-for="(_, slot) of $scopedSlots" v-slot:[slot]="scope"><slot :name="slot" v-bind="scope"/></template>
  </b-table>
</wrapper>

Vue 2.5

Paul's answer


原答案

您需要像这样指定插槽:

<wrapper>
  <b-table v-bind="$attrs" v-on="$listeners">
    <!-- Pass on the default slot -->
    <slot/>

    <!-- Pass on any named slots -->
    <slot name="foo" slot="foo"/>
    <slot name="bar" slot="bar"/>

    <!-- Pass on any scoped slots -->
    <template slot="baz" slot-scope="scope"><slot name="baz" v-bind="scope"/></template>
  </b-table>
</wrapper>

渲染功能

render(h) {
  const children = Object.keys(this.$slots).map(slot => h('template', { slot }, this.$slots[slot]))
  return h('wrapper', [
    h('b-table', {
      attrs: this.$attrs,
      on: this.$listeners,
      scopedSlots: this.$scopedSlots,
    }, children)
  ])
}

您可能还想在组件上将 inheritAttrs 设置为 false。

【讨论】:

  • 在使用命名插槽时(在 vue 2.6.11 上),“Vue 2.6”的答案对我不起作用。如果我删除部分="scope"v-bind="scope",它适用于命名插槽,但我不能再将它用于作用域插槽:(
  • 我在 2.6 中遇到了同样的问题。似乎问题在于尝试访问非范围插槽的范围。 Sergey 在下面的回答对我有用。
【解决方案2】:

我一直在使用v-for 自动传递任何(和所有)插槽,如下所示。这种方法的好处是您不需要知道必须传递哪些插槽,包括默认插槽。传递给包装器的任何槽都将被传递。

<wrapper>
  <b-table v-bind="$attrs" v-on="$listeners">

    <!-- Pass on all named slots -->
    <slot v-for="slot in Object.keys($slots)" :name="slot" :slot="slot"/>

    <!-- Pass on all scoped slots -->
    <template v-for="slot in Object.keys($scopedSlots)" :slot="slot" slot-scope="scope"><slot :name="slot" v-bind="scope"/></template>

  </b-table>
</wrapper>

【讨论】:

  • 这很好用!如果可以的话,我现在就给你买杯咖啡:)
【解决方案3】:

这里是 vue >2.6 的更新语法,带有作用域插槽和常规插槽,感谢 Nikita-Polyakov,link to discussion

<!-- pass through scoped slots -->
<template v-for="(_, scopedSlotName) in $scopedSlots" v-slot:[scopedSlotName]="slotData">
  <slot :name="scopedSlotName" v-bind="slotData" />
</template>

<!-- pass through normal slots -->
<template v-for="(_, slotName) in $slots" v-slot:[slotName]>
  <slot :name="slotName" />
</template>

<!-- after iterating over slots and scopedSlots, you can customize them like this -->
<template v-slot:overrideExample>
    <slot name="overrideExample" />
    <span>This text content goes to overrideExample slot</span>
</template>

【讨论】:

  • 谢谢,谢谢,谢谢
  • 这似乎是 Vue 2.6 中唯一可行的方法。似乎尝试从常规的非作用域插槽访问 slotData 只会阻止它工作。它似乎是内部的,为未定义的 slotProps (v-bind="slotProps || {}") 添加后备不起作用。必须按此顺序执行此操作,以便替换“损坏”的插槽。
猜你喜欢
  • 2020-02-14
  • 2018-09-14
  • 2020-05-24
  • 1970-01-01
  • 2021-01-20
  • 2020-04-23
  • 2021-02-06
  • 2019-01-21
  • 2017-08-19
相关资源
最近更新 更多