index.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { ref } from 'vue'
  2. /**
  3. * @param callback 组件关闭时的回调
  4. * @returns
  5. */
  6. export function useComponent(callback?: (componentName?: string) => void) {
  7. // 组件名
  8. const componentId = ref<string>();
  9. /**
  10. * 打开组件
  11. * @param componentName
  12. */
  13. const openComponent = (componentName: string) => {
  14. if (componentName) {
  15. console.log('打开组件:' + componentName.toString());
  16. componentId.value = componentName;
  17. } else {
  18. console.error('无效的组件名');
  19. }
  20. }
  21. /**
  22. * 关闭组件
  23. * @param isCallback
  24. */
  25. const closeComponent = (isCallback?: boolean) => {
  26. const componentName = componentId.value
  27. componentId.value = undefined;
  28. console.log('关闭组件:' + componentName);
  29. // 是否刷新数据
  30. if (isCallback && callback) {
  31. callback(componentName);
  32. }
  33. }
  34. return {
  35. componentId,
  36. openComponent,
  37. closeComponent,
  38. }
  39. }