Subscribe on changes!

this.$slots.default() 传参数无效

avatar
Nov 17th 2022

Vue version

3.2.45

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-pnyu83?file=src/components/Mask.ts

Steps to reproduce

使用 this.$slots.default?.(extendProps) 传递属性无效

代码如下:

import { defineComponent } from 'vue';

export default defineComponent({
    render() {
        const extendProps = { name: "123123", title: "asdasd" };
        const children = this.$slots.default?.(extendProps);
    }
})

What is expected?

this.$slots.default?.(extendProps) 可以正常执行

What is actually happening?

使用 cloneVNode 可以正确执行

import { cloneVNode, defineComponent } from 'vue';

export default defineComponent({
    render() {
        const extendProps = { name: "123123", title: "asdasd" };
        const children = this.$slots.default?.();
        return children && cloneVNode(children[0], extendProps);
    }
})

System Info

No response

Any additional comments?

No response

avatar
Nov 17th 2022

You misunderstand how scoped slots work. passing props to the slots function makes them available to the slot content, it does not apply them automatically. you need to do that yourself:

<Mask v-slot="extendProps">
    <Header v-bind="extendProps" />
  </Mask>

docs: https://cn.vuejs.org/guide/components/slots.html#scoped-slots

avatar
Nov 18th 2022

You misunderstand how scoped slots work. passing props to the slots function makes them available to the slot content, it does not apply them automatically. you need to do that yourself:

<Mask v-slot="extendProps">
    <Header v-bind="extendProps" />
  </Mask>

docs: https://cn.vuejs.org/guide/components/slots.html#scoped-slots

That's right! Thanks.