fix(nuxt): bind `createClientOnly` render function to ctx (#22289)

This commit is contained in:
Julien Huang 2023-07-24 10:00:31 +02:00 committed by GitHub
parent 7046930a67
commit 2e6c4519cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 1 deletions

View File

@ -34,7 +34,7 @@ export function createClientOnly<T extends ComponentOptions> (component: T) {
// override the component render (non script setup component)
clone.render = (ctx: any, ...args: any[]) => {
if (ctx.mounted$) {
const res = component.render!(ctx, ...args)
const res = component.render?.bind(ctx)(ctx, ...args)
return (res.children === null || typeof res.children === 'string')
? createElementVNode(res.type, res.props, res.children, res.patchFlag, res.dynamicProps, res.shapeFlag)
: h(res)

View File

@ -0,0 +1,11 @@
export default defineComponent({
name: 'Foo',
methods: {
getMessage () {
return 'Hello world'
}
},
render () {
return h('div', {}, this.getMessage())
}
})

View File

@ -1,5 +1,6 @@
<template>
<div>
<ClientBinding />
<ClientScript ref="clientScript" class="client-only-script" foo="bar" />
<ClientSetupScript
ref="clientSetupScript"