diff --git a/src/utils/index.ts b/src/utils/index.ts index 20952adc..37526aa5 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,5 @@ import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router'; -import type { App, Plugin } from 'vue'; +import type { App, Component } from 'vue'; import { unref } from 'vue'; import { isObject } from '/@/utils/is'; @@ -57,7 +57,7 @@ export function openWindow( } // dynamic use hook props -export function getDynamicProps(props: T): Partial { +export function getDynamicProps, U>(props: T): Partial { const ret: Recordable = {}; Object.keys(props).map((key) => { @@ -82,13 +82,29 @@ export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormal }; } -export const withInstall = (component: T, alias?: string) => { - const comp = component as any; - comp.install = (app: App) => { - app.component(comp.name || comp.displayName, component); +// https://github.com/vant-ui/vant/issues/8302 +type EventShim = { + new (...args: any[]): { + $props: { + onClick?: (...args: any[]) => void; + }; + }; +}; + +export type WithInstall = T & { + install(app: App): void; +} & EventShim; + +export type CustomComponent = Component & { displayName?: string }; + +export const withInstall = (component: T, alias?: string) => { + (component as Record).install = (app: App) => { + const compName = component.name || component.displayName; + if (!compName) return; + app.component(compName, component); if (alias) { app.config.globalProperties[alias] = component; } }; - return component as T & Plugin; + return component as WithInstall; };