fix(nuxt): allow layouts to receive custom props (#9395)

This commit is contained in:
Daniel Roe 2022-11-29 12:16:41 +00:00 committed by GitHub
parent 1865bddc7b
commit aa9aec112a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 3 deletions

View File

@ -11,6 +11,7 @@ import { appLayoutTransition as defaultLayoutTransition } from '#build/nuxt.conf
// TODO: revert back to defineAsyncComponent when https://github.com/vuejs/core/issues/6638 is resolved
const LayoutLoader = defineComponent({
inheritAttrs: false,
props: {
name: String,
...process.dev ? { hasTransition: Boolean } : {}
@ -32,14 +33,15 @@ const LayoutLoader = defineComponent({
return () => {
if (process.dev && process.client && props.hasTransition) {
vnode = h(LayoutComponent, {}, context.slots)
vnode = h(LayoutComponent, context.attrs, context.slots)
return vnode
}
return h(LayoutComponent, {}, context.slots)
return h(LayoutComponent, context.attrs, context.slots)
}
}
})
export default defineComponent({
inheritAttrs: false,
props: {
name: {
type: [String, Boolean, Object] as unknown as () => string | false | Ref<string | false>,
@ -74,7 +76,7 @@ export default defineComponent({
// We avoid rendering layout transition if there is no layout to render
return _wrapIf(Transition, hasLayout && transitionProps, {
default: () => _wrapIf(LayoutLoader, hasLayout && { key: layout.value, name: layout.value, hasTransition: process.dev ? !!transitionProps : undefined }, context.slots).default()
default: () => _wrapIf(LayoutLoader, hasLayout && { key: layout.value, name: layout.value, hasTransition: process.dev ? !!transitionProps : undefined, ...context.attrs }, context.slots).default()
}).default()
}
}

View File

@ -403,6 +403,11 @@ describe('layouts', () => {
expect(html).toContain('Custom Layout:')
await expectNoClientErrors('/with-dynamic-layout')
})
it('should allow passing custom props to a layout', async () => {
const html = await $fetch('/layouts/with-props')
expect(html).toContain('some prop was passed')
await expectNoClientErrors('/layouts/with-props')
})
})
describe('reactivity transform', () => {

View File

@ -0,0 +1,10 @@
<template>
<p>{{ someProp }}</p>
<slot />
</template>
<script lang="ts" setup>
defineProps<{
someProp: string;
}>()
</script>

View File

@ -0,0 +1,15 @@
<script setup>
definePageMeta({
layoutTransition: false
})
</script>
<template>
<div>
<NuxtLayout name="with-props" some-prop="some prop was passed">
<div>
some page content
</div>
</NuxtLayout>
</div>
</template>