Skip to content

Commit cf5fa2b

Browse files
authored
fix(getCurrentInstance): emit event (#624)
1 parent 7f9555c commit cf5fa2b

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/runtimeContext.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export declare interface ComponentInternalInstance {
145145
isDeactivated: boolean
146146
}
147147

148-
export function getCurrentVu2Instance() {
148+
export function getCurrentVue2Instance() {
149149
return currentInstance
150150
}
151151

@@ -173,6 +173,9 @@ function toVue3ComponentInstance(
173173
update: vue2Instance.$forceUpdate,
174174
uid: vue2Instance._uid,
175175

176+
// $emit is defined on prototype and it expected to be bound
177+
emit: vue2Instance.$emit.bind(vue2Instance),
178+
176179
parent: null,
177180
root: null as any,
178181
} as unknown) as ComponentInternalInstance
@@ -183,7 +186,6 @@ function toVue3ComponentInstance(
183186
'props',
184187
'attrs',
185188
'refs',
186-
'emit',
187189
'vnode',
188190
'slots',
189191
] as const

src/utils/instance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ComponentInstance } from '../component'
22
import vmStateManager from './vmStateManager'
3-
import { setCurrentInstance, getCurrentVu2Instance } from '../runtimeContext'
3+
import { setCurrentInstance, getCurrentVue2Instance } from '../runtimeContext'
44
import { Ref, isRef } from '../apis'
55
import { hasOwn, proxy, warn } from './utils'
66
import { createSlotProxy, resolveSlots } from './helper'
@@ -112,7 +112,7 @@ export function activateCurrentInstance(
112112
fn: (vm_: ComponentInstance) => any,
113113
onError?: (err: Error) => void
114114
) {
115-
let preVm = getCurrentVu2Instance()
115+
let preVm = getCurrentVue2Instance()
116116
setCurrentInstance(vm)
117117
try {
118118
return fn(vm)

test/v3/runtime-core/componentProxy.spec.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ describe('component: proxy', () => {
4545
components: {
4646
Comp,
4747
},
48-
template: '<Comp/>',
48+
data() {
49+
return {
50+
update: 0,
51+
}
52+
},
53+
template: '<Comp @update="update++"/>',
4954
}
5055
const app = createApp(Parent).mount(document.createElement('div'))
5156

@@ -60,10 +65,17 @@ describe('component: proxy', () => {
6065
instance!.parent && instance!.parent.proxy
6166
)
6267
expect(instanceProxy.$root).toBe(instance!.root.proxy)
63-
expect(instanceProxy.$emit).toBe(instance!.emit)
6468
expect(instance.isMounted).toBe(true)
6569
expect(instance.isUnmounted).toBe(false)
6670

71+
// @ts-expect-error no typings
72+
expect(app.update).toBe(0)
73+
74+
instance!.emit('update')
75+
76+
// @ts-expect-error no typings
77+
expect(app.update).toBe(1)
78+
6779
// expect(instanceProxy.$el).toBe(instance!.vnode.el)
6880
// expect(instanceProxy.$options).toBe(instance!.type)
6981

0 commit comments

Comments
 (0)