comp.ts 784 B

12345678910111213141516171819202122232425262728293031323334
  1. import { Component, DefineComponent } from "vue";
  2. import { App, Plugin, getCurrentInstance } from "vue";
  3. interface Registed {
  4. [key: string]: boolean;
  5. }
  6. let registed: Registed = {};
  7. interface Comp {
  8. displayName?: string;
  9. name?: string;
  10. }
  11. type RegisteComp = (comp: Comp & Plugin) => void;
  12. const registeComp: RegisteComp = comp => {
  13. const name = comp.displayName || comp.name;
  14. if (name && !registed[name]) {
  15. const instance = getCurrentInstance();
  16. const app = instance?.appContext.app;
  17. if (app) {
  18. app.use(comp);
  19. registed[name] = true;
  20. }
  21. }
  22. };
  23. const regComp: (comp: any) => void = comp => {
  24. const instance = getCurrentInstance();
  25. const app = instance?.appContext.app;
  26. app?.component(comp?.name, comp);
  27. };
  28. export { registeComp, regComp };