Skip to content

Commit 564a5a4

Browse files
authored
feat(createElement): allow createElement to bind vm (#920)
1 parent 5874eb5 commit 564a5a4

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/apis/createElement.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
import type { CreateElement } from 'vue'
2-
import { getVueConstructor, getCurrentInstance } from '../runtimeContext'
2+
import {
3+
getVueConstructor,
4+
getCurrentInstance,
5+
ComponentInternalInstance,
6+
} from '../runtimeContext'
37
import { defineComponentInstance } from '../utils/helper'
48
import { warn } from '../utils'
9+
import { AsyncComponent, Component } from 'vue/types/options'
10+
import { VNode, VNodeChildren, VNodeData } from 'vue/types/vnode'
11+
12+
export interface H extends CreateElement {
13+
(
14+
this: ComponentInternalInstance | null,
15+
tag?:
16+
| string
17+
| Component<any, any, any, any>
18+
| AsyncComponent<any, any, any, any>
19+
| (() => Component),
20+
children?: VNodeChildren
21+
): VNode
22+
(
23+
this: ComponentInternalInstance | null,
24+
tag?:
25+
| string
26+
| Component<any, any, any, any>
27+
| AsyncComponent<any, any, any, any>
28+
| (() => Component),
29+
data?: VNodeData,
30+
children?: VNodeChildren
31+
): VNode
32+
}
533

634
let fallbackCreateElement: CreateElement
735

8-
export const createElement = function createElement(...args: any) {
9-
const instance = getCurrentInstance()?.proxy
36+
export const createElement = function createElement(this, ...args: any) {
37+
const instance = this ? this.proxy : getCurrentInstance()?.proxy
1038
if (!instance) {
1139
__DEV__ &&
1240
warn('`createElement()` has been called outside of render function.')
@@ -20,4 +48,4 @@ export const createElement = function createElement(...args: any) {
2048
}
2149

2250
return instance.$createElement.apply(instance, args)
23-
} as CreateElement
51+
} as H

0 commit comments

Comments
 (0)