left-window.uvue 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<template>
	<view class="left-window-style">
		<view class="second-menu">
			<keep-alive>
        <component :is="active" :hasLeftWin="hasLeftWin" :leftWinActive="leftWinActive"></component>
			</keep-alive>
		</view>
	</view>
</template>

<script lang="uts">
	import componentPage from '@/pages/tabBar/component.uvue'
	import API from '@/pages/tabBar/API.uvue'
	import CSS from '@/pages/tabBar/CSS.uvue'
	import templatePage from '@/pages/tabBar/template.uvue'
  import { state, setMatchLeftWindow, setActive, setLeftWinActive } from '@/store/index.uts'
	let isPcObserver, isPhoneObserver
	export default {
		data() {
			return {
				nav: [
					'component',
					'API',
					'CSS',
					'template'
				],
				isPC: false
			}
		},
		components: {
			componentPage,
			API,
			CSS,
			templatePage
		},
		computed: {
Anne_LXM's avatar
Anne_LXM 已提交
37
      active():string{
38 39
        return state.active
      },
Anne_LXM's avatar
Anne_LXM 已提交
40
      hasLeftWin():boolean{
41 42
        return !state.noMatchLeftWindow
      },
Anne_LXM's avatar
Anne_LXM 已提交
43
      leftWinActive():string{
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        return state.leftWinActive.split('/')[3]
      }
		},
		mounted() {
			isPcObserver = uni.createMediaQueryObserver(this)
			isPhoneObserver = uni.createMediaQueryObserver(this)
			isPcObserver.observe({
				minWidth: 768
			}, matched => {
				this.isPC = matched
				const pageUrl = this.$route.path
				if (!pageUrl) return
				const pageName = this.$route.path.split('/')[4]
        if(pageUrl === '/'){
          uni.reLaunch({
          	url: "/pages/component/public-properties/public-properties"
          })
        }else{
          uni.reLaunch({
          	url: pageUrl
          })
        }
			})
			isPhoneObserver.observe({
				maxWidth: 767
			}, matched => {
				this.isPC = !matched
				if (matched) {
					const pageUrl = this.$route.path
					const tabbarName = this.$route.path.split('/')[2]
					const tabbarUrl = tabbarName && (tabbarName === 'component' ? '/' : `/pages/tabBar/${tabbarName}`)
					uni.switchTab({
						url: tabbarUrl,
						success(e) {
							uni.navigateTo({
								url: pageUrl
							})
						}
					})
				}
			})
		},
		unmounted() {
			isPcObserver.disconnect()
			isPhoneObserver.disconnect()
		},
		watch: {
			isPC: {
				immediate: true,
				handler(newMatches:boolean) {
					setMatchLeftWindow(newMatches)
				}
			},
			$route(newRoute) {
				this.handlerRoute(newRoute)
			}
		},
		methods: {
			handlerRoute(newRoute) {
				if (this.isPC) {
					if (newRoute.path === '/') {
						uni.redirectTo({
							url: '/pages/component/public-properties/public-properties'
						})
					} else if (!newRoute.matched.length) {
						uni.redirectTo({
							url: '/pages/error/404'
						})
					} else {
						setLeftWinActive(newRoute.path)
						let active = newRoute.path.split('/')[2]
						if (this.nav.includes(active)) {
							if (active === 'component') {
								active = 'componentPage'
							}
							if (active === 'template') {
								active = 'templatePage'
							}
							setActive(active)
						}
					}
				}
			}
		}
	}
</script>

<style>
	.left-window-style {
		min-height: calc(100vh - var(--top-window-height));
		background-color: #f8f8f8;
	}
	.second-menu {
		width: 350px;
		background-color: #F8F8F8;
	}
</style>