top-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 37 38 39 40 41 42 43 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
<template>
  <view class="top-window-header">
    <view class="left-header">
      <navigator class="left-header" open-type="reLaunch" url="/pages/component/global-properties/global-properties">
        <image src="/static/logo.png" class="logo" mode="heightFix"></image>
        <text>hello uni-app</text>
      </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 = {
    tabBar : string;
    index : string;
  }
  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',
          index: '/pages/component/global-properties/global-properties'
        }, {
          tabBar: '/pages/tabBar/API',
          index: '/pages/API/get-app/get-app'
        }, {
          tabBar: '/pages/tabBar/CSS',
          index: '/pages/CSS/layout/width'
        }, {
          tabBar: '/pages/tabBar/template',
          index: '/pages/template/long-list/long-list'
        }] as IndexPageItem[]
      }
    },
    watch: {
      $route: {
        immediate: true,
        handler(newRoute) {
          const width = uni.getSystemInfoSync().windowWidth
          if (width <= 768) {
            return
          }
          let path = newRoute.path
          let category
          if (path === '/') {
            category = 'component'
          } 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({
              url: indexPageItem.index
            })
            category = path.split('/')[3]
          } 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({
              url: item.index
            })
          }
        }
      }
    }
  }
</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;
  }
121
</style>