Skip to content

Commit

Permalink
feat(createElement): allow createElement to bind vm (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinatoHikari authored Apr 25, 2022
1 parent 5874eb5 commit 564a5a4
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/apis/createElement.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
import type { CreateElement } from 'vue'
import { getVueConstructor, getCurrentInstance } from '../runtimeContext'
import {
getVueConstructor,
getCurrentInstance,
ComponentInternalInstance,
} from '../runtimeContext'
import { defineComponentInstance } from '../utils/helper'
import { warn } from '../utils'
import { AsyncComponent, Component } from 'vue/types/options'
import { VNode, VNodeChildren, VNodeData } from 'vue/types/vnode'

export interface H extends CreateElement {
(
this: ComponentInternalInstance | null,
tag?:
| string
| Component<any, any, any, any>
| AsyncComponent<any, any, any, any>
| (() => Component),
children?: VNodeChildren
): VNode
(
this: ComponentInternalInstance | null,
tag?:
| string
| Component<any, any, any, any>
| AsyncComponent<any, any, any, any>
| (() => Component),
data?: VNodeData,
children?: VNodeChildren
): VNode
}

let fallbackCreateElement: CreateElement

export const createElement = function createElement(...args: any) {
const instance = getCurrentInstance()?.proxy
export const createElement = function createElement(this, ...args: any) {
const instance = this ? this.proxy : getCurrentInstance()?.proxy
if (!instance) {
__DEV__ &&
warn('`createElement()` has been called outside of render function.')
Expand All @@ -20,4 +48,4 @@ export const createElement = function createElement(...args: any) {
}

return instance.$createElement.apply(instance, args)
} as CreateElement
} as H

0 comments on commit 564a5a4

Please sign in to comment.