Skip to content

Commit

Permalink
fix(router-view): return one node when possible
Browse files Browse the repository at this point in the history
Fix #537
  • Loading branch information
posva committed Dec 2, 2020
1 parent 70692c3 commit d18e500
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/RouterView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
AllowedComponentProps,
ComponentCustomProps,
watch,
Slot,
} from 'vue'
import {
RouteLocationNormalized,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -133,14 +132,19 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
return (
// pass the vnode to the slot as a prop.
// h and <component :is="..."> 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
/**
Expand Down

0 comments on commit d18e500

Please sign in to comment.