2021-07-25 15:26:00 +08:00
|
|
|
import type { SFCWithInstall } from './types'
|
|
|
|
|
2021-09-12 19:18:14 +08:00
|
|
|
export const withInstall = <T, E extends Record<string, any>>(
|
|
|
|
main: T,
|
|
|
|
extra?: E
|
|
|
|
) => {
|
2021-09-22 01:19:35 +08:00
|
|
|
;(main as SFCWithInstall<T>).install = (app): void => {
|
2021-09-12 19:18:14 +08:00
|
|
|
for (const comp of [main, ...Object.values(extra ?? {})]) {
|
|
|
|
app.component(comp.name, comp)
|
|
|
|
}
|
2021-07-25 15:26:00 +08:00
|
|
|
}
|
2021-09-12 19:18:14 +08:00
|
|
|
|
|
|
|
if (extra) {
|
|
|
|
for (const [key, comp] of Object.entries(extra)) {
|
|
|
|
;(main as any)[key] = comp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return main as SFCWithInstall<T> & E
|
2021-07-25 15:26:00 +08:00
|
|
|
}
|
2021-09-22 01:19:35 +08:00
|
|
|
|
|
|
|
export const withInstallFunction = <T>(fn: T, name: string) => {
|
|
|
|
;(fn as SFCWithInstall<T>).install = (app) => {
|
|
|
|
app.config.globalProperties[name] = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn as SFCWithInstall<T>
|
|
|
|
}
|