top-window.uvue 3.3 KB
Newer Older
1 2 3 4
<template>
  <view class="top-window-header">
    <view class="left-header">
      <navigator class="left-header" open-type="reLaunch" url="/pages/component/global-properties/global-properties">
5 6
        <image src="/static/uni.png" class="logo" mode="heightFix"></image>
        <text>hello uni-app x</text>
7 8 9 10 11 12 13 14 15
      </navigator>
    </view>
    <custom-tab-bar class="tab-bar-flex" direction="horizontal" :show-icon="false" :selected="current"
      @onTabItemTap="toSecondMenu" />
  </view>
</template>

<script lang="uts">
  type IndexPageItem = {
16 17
    tabBar : string.PageURIString;
    indexPageUrl : string.PageURIString;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  }
  type OnTabItemTapEvent = {
    pagePath : string;
    text : string;
    index : number;
  }
  export default {
    data() {
      return {
        selected: {
          component: 0,
          API: 1,
          CSS: 2,
          template: 3
        },
        current: 0,
        indexPage: [{
          tabBar: '/pages/tabBar/component',
36
          indexPageUrl: '/pages/component/global-properties/global-properties'
37 38
        }, {
          tabBar: '/pages/tabBar/API',
39
          indexPageUrl: '/pages/API/get-app/get-app'
40 41
        }, {
          tabBar: '/pages/tabBar/CSS',
42
          indexPageUrl: '/pages/CSS/layout/width'
43 44
        }, {
          tabBar: '/pages/tabBar/template',
45
          indexPageUrl: '/pages/template/slider-100/slider-100'
46 47 48 49 50
        }] as IndexPageItem[]
      }
    },
    watch: {
      $route: {
雪洛's avatar
雪洛 已提交
51
        // immediate: true,
52 53 54 55 56 57 58 59
        handler(newRoute) {
          const width = uni.getSystemInfoSync().windowWidth
          if (width <= 768) {
            return
          }
          let path = newRoute.path
          let category
          if (path === '/') {
雪洛's avatar
雪洛 已提交
60 61 62 63 64
            category = 'component'
            uni.redirectTo({
              url: '/pages/component/global-properties/global-properties'
            })
            return
65 66 67 68 69 70 71
          } else if (path.indexOf('/pages/tabBar') === 0) {
            const indexPageItem = this.indexPage.find(item => item.tabBar === path)
            if (!indexPageItem) {
              console.error('Invalid page path: ', path)
              return
            }
            uni.redirectTo({
72
              url: indexPageItem.indexPageUrl
雪洛's avatar
雪洛 已提交
73 74
            })
            return
75 76 77 78 79 80 81 82 83 84 85 86 87 88
          } else {
            category = path.split('/')[2]
          }
          this.current = this.selected[category]
        }
      }
    },
    methods: {
      // UniEvent类型报错,临时处理
      toSecondMenu(e : OnTabItemTapEvent) {
        const activeTabBar = '/' + e.pagePath
        for (const item of this.indexPage) {
          if (activeTabBar === item.tabBar) {
            uni.redirectTo({
89
              url: item.indexPageUrl
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
            })
          }
        }
      }
    }
  }
</script>

<style>
  .top-window-header {
    height: 60px;
    padding: 0 15px;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    border-bottom: 1px solid #e1e1e1;
    background-color: #FFFFFF;
  }

  .left-header {
    flex-direction: row;
    align-items: center;
    flex: 1;
  }

  .logo {
    height: 30px;
    width: 30px;
    margin-right: 8px;
  }

  .tab-bar-flex {
    width: 360px;
  }
125
</style>