Skip to content

Commit 2370166

Browse files
committed
Revert "refactor(directives): remove binding.instance"
This reverts commit 52cc7e8.
1 parent 19228a4 commit 2370166

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

packages/runtime-core/__tests__/directives.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
DirectiveBinding,
1010
nextTick
1111
} from '@vue/runtime-test'
12+
import { currentInstance, ComponentInternalInstance } from '../src/component'
1213

1314
describe('directives', () => {
1415
it('should work', async () => {
@@ -17,6 +18,7 @@ describe('directives', () => {
1718
function assertBindings(binding: DirectiveBinding) {
1819
expect(binding.value).toBe(count.value)
1920
expect(binding.arg).toBe('foo')
21+
expect(binding.instance).toBe(_instance && _instance.proxy)
2022
expect(binding.modifiers && binding.modifiers.ok).toBe(true)
2123
}
2224

@@ -105,9 +107,13 @@ describe('directives', () => {
105107
unmounted
106108
}
107109

110+
let _instance: ComponentInternalInstance | null = null
108111
let _vnode: VNode | null = null
109112
let _prevVnode: VNode | null = null
110113
const Comp = {
114+
setup() {
115+
_instance = currentInstance
116+
},
111117
render() {
112118
_prevVnode = _vnode
113119
_vnode = withDirectives(h('div', count.value), [
@@ -147,6 +153,7 @@ describe('directives', () => {
147153
function assertBindings(binding: DirectiveBinding) {
148154
expect(binding.value).toBe(count.value)
149155
expect(binding.arg).toBe('foo')
156+
expect(binding.instance).toBe(_instance && _instance.proxy)
150157
expect(binding.modifiers && binding.modifiers.ok).toBe(true)
151158
}
152159

@@ -160,9 +167,13 @@ describe('directives', () => {
160167
expect(prevVNode).toBe(_prevVnode)
161168
}) as DirectiveHook)
162169

170+
let _instance: ComponentInternalInstance | null = null
163171
let _vnode: VNode | null = null
164172
let _prevVnode: VNode | null = null
165173
const Comp = {
174+
setup() {
175+
_instance = currentInstance
176+
},
166177
render() {
167178
_prevVnode = _vnode
168179
_vnode = withDirectives(h('div', count.value), [
@@ -196,6 +207,7 @@ describe('directives', () => {
196207
function assertBindings(binding: DirectiveBinding) {
197208
expect(binding.value).toBe(count.value)
198209
expect(binding.arg).toBe('foo')
210+
expect(binding.instance).toBe(_instance && _instance.proxy)
199211
expect(binding.modifiers && binding.modifiers.ok).toBe(true)
200212
}
201213

@@ -284,6 +296,7 @@ describe('directives', () => {
284296
unmounted
285297
}
286298

299+
let _instance: ComponentInternalInstance | null = null
287300
let _vnode: VNode | null = null
288301
let _prevVnode: VNode | null = null
289302

@@ -294,6 +307,9 @@ describe('directives', () => {
294307
}
295308

296309
const Comp = {
310+
setup() {
311+
_instance = currentInstance
312+
},
297313
render() {
298314
return withDirectives(h(Child, { count: count.value }), [
299315
[

packages/runtime-core/src/directives.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ import { VNode } from './vnode'
1515
import { isFunction, EMPTY_OBJ, makeMap, EMPTY_ARR } from '@vue/shared'
1616
import { warn } from './warning'
1717
import { ComponentInternalInstance } from './component'
18+
import { currentRenderingInstance } from './componentRenderUtils'
1819
import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
20+
import { ComponentPublicInstance } from './componentProxy'
1921

2022
export interface DirectiveBinding {
23+
instance: ComponentPublicInstance | null
2124
value: any
2225
oldValue: any
2326
arg?: string
@@ -105,9 +108,14 @@ export function withDirectives<T extends VNode>(
105108
vnode: T,
106109
directives: DirectiveArguments
107110
): T {
111+
const internalInstance = currentRenderingInstance
112+
if (internalInstance === null) {
113+
__DEV__ && warn(`withDirectives can only be used inside render functions.`)
114+
return vnode
115+
}
116+
const instance = internalInstance.proxy
108117
const props = vnode.props || (vnode.props = {})
109-
const bindings: DirectiveBinding[] =
110-
vnode.dirs || (vnode.dirs = new Array(directives.length))
118+
const bindings = vnode.dirs || (vnode.dirs = new Array(directives.length))
111119
const injected: Record<string, true> = {}
112120
for (let i = 0; i < directives.length; i++) {
113121
let [dir, value, arg, modifiers = EMPTY_OBJ] = directives[i]
@@ -119,6 +127,7 @@ export function withDirectives<T extends VNode>(
119127
}
120128
bindings[i] = {
121129
dir,
130+
instance,
122131
value,
123132
oldValue: void 0,
124133
arg,

0 commit comments

Comments
 (0)