install.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { NOOP } from '@vue/shared'
  2. import type { App } from 'vue'
  3. import type { SFCInstallWithContext, SFCWithInstall } from './typescript'
  4. export const withInstall = <T, E extends Record<string, any>>(main: T, extra?: E) => {
  5. ;(main as SFCWithInstall<T>).install = (app): void => {
  6. for (const comp of [main, ...Object.values(extra ?? {})]) {
  7. app.component(comp.name, comp)
  8. }
  9. }
  10. if (extra) {
  11. for (const [key, comp] of Object.entries(extra)) {
  12. ;(main as any)[key] = comp
  13. }
  14. }
  15. return main as SFCWithInstall<T> & E
  16. }
  17. export const withInstallFunction = <T>(fn: T, name: string) => {
  18. ;(fn as SFCWithInstall<T>).install = (app: App) => {
  19. ;(fn as SFCInstallWithContext<T>)._context = app._context
  20. app.config.globalProperties[name] = fn
  21. }
  22. return fn as SFCInstallWithContext<T>
  23. }
  24. export const withInstallDirective = <T>(directive: T, name: string) => {
  25. ;(directive as SFCWithInstall<T>).install = (app: App): void => {
  26. app.directive(name, directive)
  27. }
  28. return directive as SFCWithInstall<T>
  29. }
  30. export const withNoopInstall = <T>(component: T) => {
  31. ;(component as SFCWithInstall<T>).install = NOOP
  32. return component as SFCWithInstall<T>
  33. }