Vue - how to pass down slots inside wrapper component?(Vue - 如何在包装器组件中传递插槽?)
问题描述
所以我创建了一个简单的包装器组件,其模板如下:
So I've created a simple wrapper component with template like:
<wrapper>
<b-table v-bind="$attrs" v-on="$listeners"></b-table>
</wrapper>
使用 $attrs
和 $listeners
传递道具和事件.
工作正常,但是包装器如何将 <b-table>
命名槽代理给孩子?
using $attrs
and $listeners
to pass down props and events.
Works fine, but how can the wrapper proxy the <b-table>
named slots to the child?
推荐答案
Vue 3
与下面的 Vue 2.6 示例相同,除了:
Same as the Vue 2.6 example below except:
$listeners
已合并到$attrs
中,因此不再需要v-on=$listeners"
.请参阅迁移指南.$scopedSlots
现在只是$slots
.请参阅迁移指南.
$listeners
has been merged into$attrs
sov-on="$listeners"
is no longer necessary. See the migration guide.$scopedSlots
is now just$slots
. See migration guide.
Vue 2.6(v-slot 语法)
所有普通槽都会被添加到作用域槽中,所以你只需要这样做:
All ordinary slots will be added to scoped slots, so you only need to do this:
<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
请参阅 保罗的回答.
原答案
您需要像这样指定插槽:
You need to specify the slots like this:
<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组件.
You probably also want to set inheritAttrs
to false on the component.
这篇关于Vue - 如何在包装器组件中传递插槽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Vue - 如何在包装器组件中传递插槽?


- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06