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

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

使用$attrs$listeners传递props 和事件

推荐答案

Vue 3

与下面的Vue 2.6示例相同,只是:


Vue 2.6 (v-slot syntax)

所有普通插槽都将添加到作用域插槽中,因此您只需执行以下操作:

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


Original 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 function

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.js相关问答推荐

roduction:main@HEAD失败阶段构建站点期间失败:构建脚本返回非零退出代码:2

点击屏幕的任何位置都会禁用v-overlay

在移动版本中使用标准的 v-data-table

如何 for each 动态创建的按钮/文本字段设置唯一变量?

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

在 Ajax 函数中访问 Vue.js 组件属性

nuxt.js - 预加载 .woff 字体加载为@font-face

如何在 Nativescript 应用程序中调用 axios/api 调用

是否可以让 html-webpack-plugin 从 css 生成