Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(createElement): allow createElement to bind vm #920

Merged
merged 1 commit into from Apr 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/apis/createElement.ts
@@ -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