From 20d621996ac0433c11630dd47d7b6cfb42033a0f Mon Sep 17 00:00:00 2001 From: MinatoHikari <35342316+MinatoHikari@users.noreply.github.com> Date: Fri, 22 Apr 2022 14:11:20 +0800 Subject: [PATCH] fix(h): h can bind vm --- src/apis/createElement.ts | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/apis/createElement.ts b/src/apis/createElement.ts index 79ee9abc..c9c196aa 100644 --- a/src/apis/createElement.ts +++ b/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 + | AsyncComponent + | (() => Component), + children?: VNodeChildren + ): VNode + ( + this: ComponentInternalInstance | null, + tag?: + | string + | Component + | AsyncComponent + | (() => 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.') @@ -20,4 +48,4 @@ export const createElement = function createElement(...args: any) { } return instance.$createElement.apply(instance, args) -} as CreateElement +} as H