From d18e500da2ed017be30871628a5cc59324bec15c Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 2 Dec 2020 19:03:38 +0100 Subject: [PATCH] fix(router-view): return one node when possible Fix #537 --- src/RouterView.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/RouterView.ts b/src/RouterView.ts index 86fff50bd..323b2d14f 100644 --- a/src/RouterView.ts +++ b/src/RouterView.ts @@ -12,6 +12,7 @@ import { AllowedComponentProps, ComponentCustomProps, watch, + Slot, } from 'vue' import { RouteLocationNormalized, @@ -100,9 +101,7 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({ const currentName = props.name if (!ViewComponent) { - return slots.default - ? slots.default({ Component: ViewComponent, route }) - : null + return normalizeSlot(slots.default, { Component: ViewComponent, route }) } // props from route configuration @@ -133,14 +132,19 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({ return ( // pass the vnode to the slot as a prop. // h and both accept vnodes - slots.default - ? slots.default({ Component: component, route }) - : component + normalizeSlot(slots.default, { Component: component, route }) || + component ) } }, }) +function normalizeSlot(slot: Slot | undefined, data: any) { + if (!slot) return null + const slotContent = slot(data) + return slotContent.length === 1 ? slotContent[0] : slotContent +} + // export the public type for h/tsx inference // also to avoid inline import() in generated d.ts files /**