createSubApp.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <template>
  2. <component :is="component" />
  3. </template>
  4. <script setup lang="ts">
  5. import { ref, onBeforeUnmount, onBeforeMount, watch, nextTick } from 'vue';
  6. import { useRoute } from 'vue-router';
  7. import { getActions } from '/@/qiankun/state';
  8. import { activeApps } from '/@/qiankun/index';
  9. const route = useRoute();
  10. const component = ref<any>(null);
  11. async function resetComponent() {
  12. component.value = null;
  13. return nextTick(() => {
  14. return setTimeout(async () => {
  15. const appType = route.path.split('/')[1];
  16. if (appType === 'micro-need-air') {
  17. component.value = (await import('./needAir.vue')).default;
  18. } else if (appType === 'micro-vent-doc') {
  19. component.value = (await import('./ventDoc.vue')).default;
  20. } else if (appType === 'micro-vent-3dModal' || appType === 'micro-vent-2dModal') {
  21. if (route.query['isNoReverse'] != '1') {
  22. if (VENT_PARAM['is2DModel']) {
  23. component.value = (await import('./ventModal2D.vue')).default;
  24. } else {
  25. component.value = (await import('./ventModal.vue')).default;
  26. }
  27. } else if (appType === 'micro-vent-3dModal') {
  28. component.value = (await import('./ventModal.vue')).default;
  29. } else if (appType === 'micro-vent-2dModal') {
  30. component.value = (await import('./ventModal2D.vue')).default;
  31. }
  32. }
  33. }, 0);
  34. });
  35. }
  36. watch(
  37. () => [route.query, route.path],
  38. async ([newQuery, newPath], [oldQuery, oldPath]) => {
  39. // 这里判断
  40. if (newPath !== oldPath) {
  41. await resetComponent();
  42. } else if (newQuery !== oldQuery) {
  43. const actions = getActions();
  44. const appType = route.path.split('/')[1];
  45. activeApps[appType].mountPromise.then(() => {
  46. actions.setGlobalState({ currentRouter: route });
  47. });
  48. }
  49. }
  50. );
  51. onBeforeMount(async () => {
  52. await resetComponent();
  53. });
  54. onBeforeUnmount(() => {
  55. component.value = null;
  56. });
  57. </script>