Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): ensure that binding.instance in custom directive hooks properly exposes properties on closed instances (fix: #5018) #5022

Merged
merged 1 commit into from Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion packages/runtime-core/__tests__/directives.spec.ts
Expand Up @@ -7,7 +7,8 @@ import {
DirectiveHook,
VNode,
DirectiveBinding,
nextTick
nextTick,
defineComponent
} from '@vue/runtime-test'
import { currentInstance, ComponentInternalInstance } from '../src/component'

Expand Down Expand Up @@ -395,4 +396,29 @@ describe('directives', () => {
expect(beforeUpdate).toHaveBeenCalledTimes(1)
expect(count.value).toBe(1)
})

test('should receive exposeProxy for closed instances', async () => {
let res: string
const App = defineComponent({
setup(_, { expose }) {
expose({
msg: 'Test'
})

return () =>
withDirectives(h('p', 'Lore Ipsum'), [
[
{
mounted(el, { instance }) {
res = (instance as any).msg as string
}
}
]
])
}
})
const root = nodeOps.createElement('div')
render(h(App), root)
expect(res!).toBe('Test')
})
})
6 changes: 4 additions & 2 deletions packages/runtime-core/src/directives.ts
Expand Up @@ -14,7 +14,7 @@ return withDirectives(h(comp), [
import { VNode } from './vnode'
import { isFunction, EMPTY_OBJ, makeMap } from '@vue/shared'
import { warn } from './warning'
import { ComponentInternalInstance, Data } from './component'
import { ComponentInternalInstance, Data, getExposeProxy } from './component'
import { currentRenderingInstance } from './componentRenderContext'
import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
import { ComponentPublicInstance } from './componentPublicInstance'
Expand Down Expand Up @@ -93,7 +93,9 @@ export function withDirectives<T extends VNode>(
__DEV__ && warn(`withDirectives can only be used inside render functions.`)
return vnode
}
const instance = internalInstance.proxy
const instance =
(getExposeProxy(internalInstance) as ComponentPublicInstance) ||
internalInstance.proxy
const bindings: DirectiveBinding[] = vnode.dirs || (vnode.dirs = [])
for (let i = 0; i < directives.length; i++) {
let [dir, value, arg, modifiers = EMPTY_OBJ] = directives[i]
Expand Down