diff --git a/packages/vue/src/shared/fragment.ts b/packages/vue/src/shared/fragment.ts index ca92820c570..9b40f7fffc2 100644 --- a/packages/vue/src/shared/fragment.ts +++ b/packages/vue/src/shared/fragment.ts @@ -22,8 +22,10 @@ if (isVue2) { name: 'frag', }, ], + attrs: vm.$attrs, + scopedSlots: vm.$scopedSlots, }, - vm?.$slots?.default + vm?.$scopedSlots?.default?.(vm.$attrs) ) }, } diff --git a/packages/vue/src/shared/h.ts b/packages/vue/src/shared/h.ts index 804fe16385a..be7eccdafb7 100644 --- a/packages/vue/src/shared/h.ts +++ b/packages/vue/src/shared/h.ts @@ -23,20 +23,30 @@ const compatibleCreateElement = ( data?: VNodeData, components?: VNodeChildren ) => VNode - const scopedSlots = {} + const scopedSlots = components // 默认全部作为 scopedSlots 处理 const children = [] + + /** + * scopedSlots 不会映射为slots,所以这里手动映射一遍 + * 主要为了解决 slots.x 问题 + */ Object.keys(components).forEach((key) => { const func = components[key] - if (typeof func === 'function') { - if (func.length !== 0) { - scopedSlots[key] = func - } else if (key !== 'default') { - scopedSlots[key] = func - // compatible with slots usage - children.push(hInVue2(FragmentComponent, { slot: key }, func())) - } else { - children.push(func()) - } + + // 转换为 slots 传递 + if (typeof func === 'function' && func.length === 0) { + /** + * func 参数为0的判断不准确,因为composition-api包了一层,导致全部为0 + * try catch 解决scoped slots 转换参数异常问题 + * */ + try { + const child = func() + children.push( + key === 'default' + ? child + : hInVue2(FragmentComponent, { slot: key }, child) + ) + } catch (error) {} } }) const newData = Object.assign({}, data)