Skip to content

Commit

Permalink
fix(getCurrentInstance): emit event (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax committed Jan 6, 2021
1 parent 7f9555c commit cf5fa2b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/runtimeContext.ts
Expand Up @@ -145,7 +145,7 @@ export declare interface ComponentInternalInstance {
isDeactivated: boolean
}

export function getCurrentVu2Instance() {
export function getCurrentVue2Instance() {
return currentInstance
}

Expand Down Expand Up @@ -173,6 +173,9 @@ function toVue3ComponentInstance(
update: vue2Instance.$forceUpdate,
uid: vue2Instance._uid,

// $emit is defined on prototype and it expected to be bound
emit: vue2Instance.$emit.bind(vue2Instance),

parent: null,
root: null as any,
} as unknown) as ComponentInternalInstance
Expand All @@ -183,7 +186,6 @@ function toVue3ComponentInstance(
'props',
'attrs',
'refs',
'emit',
'vnode',
'slots',
] as const
Expand Down
4 changes: 2 additions & 2 deletions src/utils/instance.ts
@@ -1,6 +1,6 @@
import { ComponentInstance } from '../component'
import vmStateManager from './vmStateManager'
import { setCurrentInstance, getCurrentVu2Instance } from '../runtimeContext'
import { setCurrentInstance, getCurrentVue2Instance } from '../runtimeContext'
import { Ref, isRef } from '../apis'
import { hasOwn, proxy, warn } from './utils'
import { createSlotProxy, resolveSlots } from './helper'
Expand Down Expand Up @@ -112,7 +112,7 @@ export function activateCurrentInstance(
fn: (vm_: ComponentInstance) => any,
onError?: (err: Error) => void
) {
let preVm = getCurrentVu2Instance()
let preVm = getCurrentVue2Instance()
setCurrentInstance(vm)
try {
return fn(vm)
Expand Down
16 changes: 14 additions & 2 deletions test/v3/runtime-core/componentProxy.spec.ts
Expand Up @@ -45,7 +45,12 @@ describe('component: proxy', () => {
components: {
Comp,
},
template: '<Comp/>',
data() {
return {
update: 0,
}
},
template: '<Comp @update="update++"/>',
}
const app = createApp(Parent).mount(document.createElement('div'))

Expand All @@ -60,10 +65,17 @@ describe('component: proxy', () => {
instance!.parent && instance!.parent.proxy
)
expect(instanceProxy.$root).toBe(instance!.root.proxy)
expect(instanceProxy.$emit).toBe(instance!.emit)
expect(instance.isMounted).toBe(true)
expect(instance.isUnmounted).toBe(false)

// @ts-expect-error no typings
expect(app.update).toBe(0)

instance!.emit('update')

// @ts-expect-error no typings
expect(app.update).toBe(1)

// expect(instanceProxy.$el).toBe(instance!.vnode.el)
// expect(instanceProxy.$options).toBe(instance!.type)

Expand Down

0 comments on commit cf5fa2b

Please sign in to comment.