我正在为我们在工作中创建的一些自定义组件创建交互式样式指南。它会遍历全局注册组件的列表,然后显示它们的 props。然后,用户可以编辑 prop 值并查看它如何影响渲染的组件。
现在我需要弄清楚如何允许与我们拥有的某些组件的插槽进行交互。例如,我们有自己的button组件,如下所示:
<template>
    <button :class="customClasses">
        <slot></slot>
    </button>
</template>
我有一个componentPreviewRenderer显示我们的自定义组件渲染的组件。它看起来像这样:
export default {
    props: {
        component: String, //The name of the global component to render
        props: Object //List of component props
    },
    render (createElement): {
        return createElement(
            this.component,
            {
                props: this.props
            }
        );
    }
}
我需要 (1) 查明我正在渲染的组件模板是否有插槽,以及 (2) 获取插槽名称列表,以便我可以将其传递到函数中createElement()并让用户编辑插槽值。例如,对于按钮组件,他们应该能够编辑控制按钮上显示的文本的“默认”插槽。
我已经看过这篇文章,但我希望能够直接从我正在渲染的 Vue 组件获取插槽名称,而不必解析 .vue 文件。我尝试过类似的事情Vue.component(this.component).$slots,但  $slots未定义。有谁知道我如何获取正在渲染的组件的插槽名称?
| 归档时间: | 
 | 
| 查看次数: | 1168 次 | 
| 最近记录: |