index.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <micro-app
  3. :key="name"
  4. style="height:100%"
  5. :name="name"
  6. :url="url"
  7. :baseroute="baseRoute"
  8. :data="{ store, router, subPath: subFullPath}"
  9. @datachange="handleDataChange"
  10. />
  11. </template>
  12. <script >
  13. // 依赖 @micro-zoe/micro-app (京东开源)
  14. // https://zeroing.jd.com/micro-app/docs.html#/
  15. import { computed, watch, onUnmounted } from 'vue'
  16. import store from '../../store'
  17. import { useMain } from '@cip/hooks/use-main'
  18. import { useRouter, useRoute } from 'vue-router'
  19. import { debounce, isArray } from '@cip/utils/util'
  20. import { judgeHiddenFramework } from './util'
  21. export default {
  22. props: {
  23. name: String,
  24. url: String,
  25. baseRoute: String,
  26. subPath: String,
  27. withoutFramework: Array
  28. },
  29. setup (props) {
  30. const { setCurrentTitle, setFrameHide } = useMain()
  31. const router = useRouter()
  32. const route = useRoute()
  33. onUnmounted(() => {
  34. console.log('Main Unmounted')
  35. })
  36. const pathChange = function (path) {
  37. if (path !== route.fullPath) {
  38. router.push(path)
  39. }
  40. }
  41. const replacePath = function (path) {
  42. if (path !== route.fullPath) {
  43. router.replace(path)
  44. }
  45. }
  46. const handleDataChange = (e) => {
  47. const { type, data } = e.detail.data || {}
  48. if (type === 'setCurrentTitle') {
  49. setCurrentTitle(data)
  50. }
  51. if (type === 'pathChange') {
  52. const path = data.substring(1)
  53. pathChange(`${props.baseRoute}${path}`)
  54. }
  55. if (type === 'replacePath') {
  56. const path = data.substring(1)
  57. replacePath(`${props.baseRoute}${path}`)
  58. }
  59. }
  60. const subFullPath = computed(() => {
  61. return route.fullPath.replace(new RegExp(`^${props.baseRoute}`), '/')
  62. })
  63. watch(() => props.subPath, (val) => {
  64. if (isArray(props.withoutFramework)) {
  65. setFrameHide(judgeHiddenFramework(props.withoutFramework, props.subPath))
  66. } else {
  67. setFrameHide(false)
  68. }
  69. }, { immediate: true })
  70. return {
  71. store,
  72. router,
  73. handleDataChange,
  74. subFullPath
  75. }
  76. }
  77. }
  78. </script>