index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div class="vue-admin-better-wrapper" :class="{ mobile }">
  3. <component
  4. :is="'vab-layout-' + theme.layout"
  5. :collapse="collapse"
  6. :device="device"
  7. :fixed-header="theme.fixedHeader"
  8. :show-tabs="theme.showTabs"
  9. />
  10. <el-backtop target="#app" />
  11. <!-- 主题组件放到layouts下防止主题切换,导致主题组件重新加载 -->
  12. <vab-theme-drawer />
  13. <vab-theme-setting />
  14. </div>
  15. </template>
  16. <script lang="ts">
  17. import { useSettingsStore } from '@/store/modules/settings'
  18. const imports = require.context('./', true, /\.vue$/)
  19. const Components: any = {}
  20. imports
  21. .keys()
  22. .filter((key) => key !== './index.vue')
  23. .forEach((key) => {
  24. Components[key.replace(/(\/|\.|index.vue)/g, '')] = imports(key).default
  25. })
  26. export default defineComponent({
  27. name: 'Layouts',
  28. components: Components,
  29. setup() {
  30. const settingsStore = useSettingsStore()
  31. const { device, collapse, theme } = storeToRefs(settingsStore)
  32. const { toggleDevice, foldSideBar, openSideBar, updateTheme } =
  33. settingsStore
  34. const mobile = ref(false)
  35. let oldLayout = theme.value.layout
  36. const resizeBody = () => {
  37. mobile.value = document.body.getBoundingClientRect().width - 1 < 992
  38. }
  39. watch(mobile, (val) => {
  40. if (val) {
  41. oldLayout = theme.value.layout
  42. foldSideBar()
  43. } else openSideBar()
  44. theme.value.layout = val ? 'vertical' : oldLayout
  45. toggleDevice(val ? 'mobile' : 'desktop')
  46. })
  47. resizeBody()
  48. updateTheme()
  49. const cleanup = useEventListener('resize', () => {
  50. resizeBody()
  51. })
  52. onUnmounted(() => {
  53. if (mobile.value) theme.value.layout = oldLayout
  54. cleanup()
  55. })
  56. return {
  57. theme,
  58. device,
  59. mobile,
  60. collapse,
  61. foldSideBar,
  62. openSideBar,
  63. toggleDevice,
  64. }
  65. },
  66. })
  67. </script>
  68. <style lang="scss" scoped>
  69. .vue-admin-better-wrapper {
  70. position: relative;
  71. width: 100%;
  72. height: 100%;
  73. [class*='vab-layout-'] {
  74. :deep() {
  75. .vab-layout-header {
  76. box-shadow: $base-box-shadow;
  77. }
  78. }
  79. &.fixed {
  80. padding-top: $base-nav-height + $base-tabs-height;
  81. }
  82. &.fixed.no-tabs-bar {
  83. padding-top: $base-nav-height;
  84. }
  85. }
  86. :deep() {
  87. .fixed-header {
  88. position: fixed;
  89. top: 0;
  90. right: 0;
  91. z-index: $base-z-index - 1;
  92. width: 100%;
  93. }
  94. .vab-main {
  95. position: relative;
  96. width: auto;
  97. min-height: 100%;
  98. margin-left: var(--el-left-menu-width);
  99. &.is-collapse-main {
  100. margin-left: $base-left-menu-width-min;
  101. .fixed-header {
  102. width: $base-right-content-width-min;
  103. }
  104. }
  105. &:not(.is-collapse-main) {
  106. .fixed-header {
  107. width: calc(100% - var(--el-left-menu-width));
  108. }
  109. }
  110. }
  111. }
  112. /* 手机端开始 */
  113. &.mobile {
  114. :deep() {
  115. .vab-layout-vertical {
  116. .el-scrollbar.vab-side-bar.is-collapse {
  117. width: 0;
  118. }
  119. .vab-main {
  120. .fixed-header {
  121. width: 100%;
  122. }
  123. margin-left: 0;
  124. }
  125. }
  126. /* 隐藏分页和页码跳转 */
  127. .el-pager,
  128. .el-pagination__jump {
  129. display: none;
  130. }
  131. }
  132. }
  133. /* 手机端结束 */
  134. }
  135. </style>