【发布时间】:2018-07-26 05:06:27
【问题描述】:
大家好,请原谅我的英语:-)
我有一个可以采用动态插槽的 Vue 组件(插槽的名称将取决于道具)。
我在几个地方使用它,并且一些插槽总是存在的。
为了避免冗余,我正在寻找一种方法来创建一个“包装”最终组件的组件,以允许我只定义附加插槽。
如果有一个“明显”的方法来实现它,我可能会错过它:-)
代码示例
没有“包装组件”
<b-table
show-empty
small
hover
[...some others and always present props...]
:items="aDataVarThatWillChangeBasedOnTheContext"
[...some others and uniq props...]
>
<template slot="same-1">
A slot that will always be present with the same content (for example, a checkbox in the first column)
</template>
<template slot="same-2">
A slot that will always be present with the same content (for example, some action buttons in the last column)
</template>
[...some others and always present slots...]
<template slot="not-the-same">
A slot that is only used in this context (for example, a duration based on a row timestamp and a timestamp picked by the user)
</template>
[...some others and uniq slots...]
</b-table>
带有“包装组件”
<my-b-table
:items="aDataVarThatWillChangeBasedOnTheContext"
>
<template slot="not-the-same">
A slot that is only used in this context (for example, a duration based on a row timestamp and a timestamp picked by the user)
</template>
</my-b-table>
注意:动态插槽名称不可预测。 如果我突然需要一个“foo”列,我应该能够传递一个“foo”插槽(在我的情况下是一个“HEAD_foo”插槽)
一些研究
我读到here:
它们(功能组件)作为包装组件也非常有用。例如,当您需要:
- 以编程方式选择其他几个组件之一来委派给
- 在将子组件、道具或数据传递给子组件之前对其进行操作
而且“在将子组件、道具或数据传递给子组件之前对其进行操作”似乎正是我所需要的。
我查看了渲染函数,但很多东西似乎没有实现,比如 v-model,我很难弄清楚如何传递动态插槽......
提前感谢您的回答!
up:在 07.03.2018 我仍然不知道如何解决这个案子
【问题讨论】:
标签: vue.js vuejs2 vue-component