left-window.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <view class="left-window-style">
  3. <view class="second-menu">
  4. <!-- <keep-alive> -->
  5. <component v-bind:is="active" :hasLeftWin="hasLeftWin" :leftWinActive="leftWinActive"></component>
  6. <!-- </keep-alive> -->
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. import componentPage from '@/pages/tabBar/component/component.nvue'
  12. import API from '@/pages/tabBar/API/API.nvue'
  13. import extUI from '@/pages/tabBar/extUI/extUI.nvue'
  14. import templatePage from '@/pages/tabBar/template/template.nvue'
  15. import {
  16. mapMutations,
  17. mapState
  18. } from 'vuex'
  19. let isPcObserver, isPhoneObserver
  20. export default {
  21. data() {
  22. return {
  23. nav: [
  24. 'component',
  25. 'API',
  26. 'extUI',
  27. 'template'
  28. ],
  29. isPC: false
  30. }
  31. },
  32. components: {
  33. componentPage,
  34. API,
  35. extUI,
  36. templatePage
  37. },
  38. computed: {
  39. ...mapState({
  40. active: state => state.active,
  41. hasLeftWin: state => !state.noMatchLeftWindow,
  42. leftWinActive: state => state.leftWinActive.split('/')[3],
  43. })
  44. },
  45. mounted() {
  46. isPcObserver = uni.createMediaQueryObserver(this)
  47. isPhoneObserver = uni.createMediaQueryObserver(this)
  48. isPcObserver.observe({
  49. minWidth: 768
  50. }, matched => {
  51. this.isPC = matched
  52. const pageUrl = this.$route.path
  53. if (!pageUrl) return
  54. const pageName = this.$route.path.split('/')[4]
  55. if (pageUrl === '/' || this.nav.includes(pageName)) {
  56. const tabbarUrl = pageName ? (pageName === 'component' ? '/' : `/pages/tabBar/${pageName}/${pageName}`) : '/'
  57. if (pageUrl === '/' || pageUrl === tabbarUrl) {
  58. uni.switchTab({
  59. url: pageUrl,
  60. })
  61. }
  62. } else {
  63. uni.reLaunch({
  64. url: pageUrl
  65. })
  66. }
  67. })
  68. isPhoneObserver.observe({
  69. maxWidth: 767
  70. }, matched => {
  71. this.isPC = !matched
  72. if (matched) {
  73. const pageUrl = this.$route.path
  74. const tabbarName = this.$route.path.split('/')[2]
  75. const tabbarUrl = tabbarName && (tabbarName === 'component' ? '/' : `/pages/tabBar/${tabbarName}/${tabbarName}`)
  76. uni.switchTab({
  77. url: tabbarUrl,
  78. success(e) {
  79. uni.navigateTo({
  80. url: pageUrl
  81. })
  82. }
  83. })
  84. }
  85. })
  86. },
  87. unmounted() {
  88. isPcObserver.disconnect()
  89. isPhoneObserver.disconnect()
  90. },
  91. watch: {
  92. isPC: {
  93. immediate: true,
  94. handler(newMatches) {
  95. this.setMatchLeftWindow(newMatches)
  96. // this.handlerRoute(this.$route)
  97. }
  98. },
  99. // #ifndef VUE3
  100. $route: {
  101. immediate: true,
  102. handler(newRoute) {
  103. this.handlerRoute(newRoute)
  104. }
  105. },
  106. // #endif
  107. // #ifdef VUE3
  108. $route(newRoute) {
  109. this.handlerRoute(newRoute)
  110. }
  111. // #endif
  112. },
  113. methods: {
  114. ...mapMutations(['setMatchLeftWindow', 'setActive', 'setLeftWinActive']),
  115. handlerRoute(newRoute) {
  116. if (this.isPC) {
  117. if (newRoute.path === '/') {
  118. // uni.redirectTo({
  119. // url: '/pages/component/view/view'
  120. // })
  121. } else if (!newRoute.matched.length) {
  122. uni.redirectTo({
  123. url: '/pages/error/404'
  124. })
  125. } else {
  126. this.setLeftWinActive(newRoute.path)
  127. let active = newRoute.path.split('/')[2]
  128. if (this.nav.includes(active)) {
  129. if (active === 'component') {
  130. active = 'componentPage'
  131. }
  132. if (active === 'template') {
  133. active = 'templatePage'
  134. }
  135. this.setActive(active)
  136. }
  137. }
  138. }
  139. },
  140. switchTab() {
  141. }
  142. }
  143. }
  144. </script>
  145. <style>
  146. @import '../common/uni-nvue.css';
  147. .left-window-style {
  148. min-height: calc(100vh - var(--top-window-height));
  149. background-color: #f8f8f8;
  150. }
  151. .second-menu {
  152. width: 350px;
  153. background-color: #F8F8F8;
  154. }
  155. .icon-image {
  156. width: 30px;
  157. height: 30px;
  158. }
  159. </style>