From 7e905fa7d762f8ea014975c6833d95a0b92dc222 Mon Sep 17 00:00:00 2001 From: timongh <46739861+timongh@users.noreply.github.com> Date: Sun, 20 Mar 2022 21:59:45 +0800 Subject: [PATCH] Fix mountVueComponent --- src/core/common-types.ts | 4 ++-- src/core/utils/index.ts | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/core/common-types.ts b/src/core/common-types.ts index ad83603ec..6f967bb62 100644 --- a/src/core/common-types.ts +++ b/src/core/common-types.ts @@ -1,4 +1,4 @@ -import { VueConstructor } from 'vue' +import { Component } from 'vue' export type Executable = () => ReturnType | Promise export type ExecutableWithParameter = ( @@ -7,7 +7,7 @@ export type ExecutableWithParameter = T extends Array ? R : T -export type VueModule = VueConstructor | { default: VueConstructor } +export type VueModule = Component | { default: Component } export type I18nDescription = string | { 'zh-CN': string; [key: string]: string } export type WithName = { name: string diff --git a/src/core/utils/index.ts b/src/core/utils/index.ts index 2ebadb58e..ca4dc150b 100644 --- a/src/core/utils/index.ts +++ b/src/core/utils/index.ts @@ -98,10 +98,15 @@ export const matchUrlPattern = (pattern: string | RegExp) => ( * @param module Vue组件模块对象 * @param target 组件的挂载目标元素, 省略时不挂载直接返回 */ -export const mountVueComponent = (module: VueModule, target?: Element | string) => { - const instance = new Vue('default' in module ? module.default : module) - // const instance = new Vue({ render: h => h('default' in module ? module.default : module) }) - return instance.$mount(target) as Vue & T +export const mountVueComponent = ( + module: VueModule, + target?: Element | string, +): Vue & T => { + const options = 'default' in module ? module.default : module + const getInstance = (o: any) => ( + o.functional ? new (Vue.extend(o))() : new Vue(o) + ) + return getInstance(options).$mount(target) as Vue & T } /** 是否处于其他网站的内嵌播放器中 */ export const isEmbeddedPlayer = () => window.location.host === 'player.bilibili.com' || document.URL.startsWith('https://www.bilibili.com/html/player.html')