diff --git a/src/core/common-types.ts b/src/core/common-types.ts index 6f967bb62..57dfb9c67 100644 --- a/src/core/common-types.ts +++ b/src/core/common-types.ts @@ -1,4 +1,4 @@ -import { Component } from 'vue' +import { Component, VueConstructor } from 'vue' export type Executable = () => ReturnType | Promise export type ExecutableWithParameter = ( @@ -7,7 +7,11 @@ export type ExecutableWithParameter = T extends Array ? R : T -export type VueModule = Component | { default: Component } +export type VueModule = + Component + | { default: Component } + | VueConstructor + | { default: VueConstructor } 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 ca4dc150b..79a47a0a1 100644 --- a/src/core/utils/index.ts +++ b/src/core/utils/index.ts @@ -102,11 +102,18 @@ 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 + const obj = 'default' in module ? module.default : module + const getInstance = (o: any) => { + if (o instanceof Function) { + // eslint-disable-next-line new-cap + return new o() + } + if (o.functional) { + return new (Vue.extend(o))() + } + return new Vue(o) + } + return getInstance(obj).$mount(target) as Vue & T } /** 是否处于其他网站的内嵌播放器中 */ export const isEmbeddedPlayer = () => window.location.host === 'player.bilibili.com' || document.URL.startsWith('https://www.bilibili.com/html/player.html')