我有一个模板:
<template>
<my-body-component inline-component>
<slot/>
</my-body-component>
</template>我希望我的body组件是一个内联组件,它包含插槽中的任何内容,但是,当我以这种方式呈现主体时:
const my-body-component= {
render(h) {
return this.$slots.default;
}
};我似乎无法访问this.$slots.default。在我自己的内联组件中获取插槽内容的好方法是什么?
我还得到:"'render‘隐式有返回类型'any’,因为它没有返回类型注释,并且在其返回表达式中直接或间接引用。“这个错误。
发布于 2020-05-20 00:23:48
使用Vue.extend({})进行类型推断。
只要插槽包含一个元素(因为Vue 2不能呈现多个片段),返回this.$slots.default就可以了。
const myBodyComponent = Vue.extend({
render(h) {
return this.$slots.default
? this.$slots.default.find(vnode => vnode.tag)!
: h('span')
}
})如果可能发生这种情况,则槽包含多个元素,相反:
return h('div', this.$slots.default)https://stackoverflow.com/questions/61899862
复制相似问题