Support to mount Component and VueConstructor

This commit is contained in:
timongh 2022-03-21 06:14:13 +08:00
parent 7e905fa7d7
commit 78635c5d81
2 changed files with 18 additions and 7 deletions

View File

@ -1,4 +1,4 @@
import { Component } from 'vue'
import { Component, VueConstructor } from 'vue'
export type Executable<ReturnType = void> = () => ReturnType | Promise<ReturnType>
export type ExecutableWithParameter<Parameters extends any[] = never[], ReturnType = void> = (
@ -7,7 +7,11 @@ export type ExecutableWithParameter<Parameters extends any[] = never[], ReturnTy
export type TestPattern = (string | RegExp)[]
export type ArrayContent<T> = T extends Array<infer R> ? 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

View File

@ -102,11 +102,18 @@ export const mountVueComponent = <T>(
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')