api-set-tabbar.vue 4.3 KB
Newer Older
W
wanganxp 已提交
1
<template>
2 3 4 5 6 7 8 9 10 11 12
  <view class="uni-padding-wrap">
    <page-head :title="title"></page-head>
    <button class="button" @click="setTabBarBadge">{{ !hasSetTabBarBadge ? '设置tab徽标' : '移除tab徽标' }}</button>
    <button class="button" @click="showTabBarRedDot">{{ !hasShownTabBarRedDot ?  '显示红点' : '移除红点'}}</button>
    <button class="button" @click="customStyle">{{ !hasCustomedStyle ? '自定义Tab样式' : '移除自定义样式'}}</button>
    <button class="button" @click="customItem">{{ !hasCustomedItem ? '自定义Tab信息' : '移除自定义信息' }}</button>
    <button class="button" @click="hideTabBar">{{ !hasHiddenTabBar ? '隐藏TabBar' : '显示TabBar' }}</button>
    <view class="btn-area">
      <!-- <button class="button" type="primary" @click="navigateBack">关闭</button> -->
    </view>
  </view>
W
wanganxp 已提交
13 14 15
</template>

<script lang="uts">
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
  export default {
    data() {
      return {
        title: 'tababr',
        hasSetTabBarBadge: false,
        hasShownTabBarRedDot: false,
        hasCustomedStyle: false,
        hasCustomedItem: false,
        hasHiddenTabBar: false
      }
    },
    destroyed() {
      if (this.hasSetTabBarBadge) {
        uni.removeTabBarBadge({
          index: 1
        })
      }
      if (this.hasShownTabBarRedDot) {
        uni.hideTabBarRedDot({
          index: 1
        })
      }
      if (this.hasHiddenTabBar) {
        uni.showTabBar()
      }
      if (this.hasCustomedStyle) {
        uni.setTabBarStyle({
          color: '#7A7E83',
          selectedColor: '#007AFF',
          backgroundColor: '#F8F8F8',
          borderStyle: 'black'
        })
      }
W
wanganxp 已提交
49

50 51 52 53 54 55
      if (this.hasCustomedItem) {
        let tabBarOptions = {
          index: 1,
          text: '接口',
          iconPath: '/static/api.png',
          selectedIconPath: '/static/apiHL.png'
雪洛's avatar
雪洛 已提交
56
        } as SetTabBarItemOptions
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
        uni.setTabBarItem(tabBarOptions)
      }
    },
    methods: {
      navigateBack() {
        this.$emit('unmount')
      },
      setTabBarBadge() {
        if (this.hasShownTabBarRedDot) {
          uni.hideTabBarRedDot({
            index: 1
          })
          this.hasShownTabBarRedDot = !this.hasShownTabBarRedDot
        }
        if (!this.hasSetTabBarBadge) {
          uni.setTabBarBadge({
            index: 1,
            text: '1'
          })
        } else {
          uni.removeTabBarBadge({
            index: 1
          })
        }
        this.hasSetTabBarBadge = !this.hasSetTabBarBadge
      },
      showTabBarRedDot() {
        if (this.hasSetTabBarBadge) {
          uni.removeTabBarBadge({
            index: 1
          })
          this.hasSetTabBarBadge = !this.hasSetTabBarBadge
        }
        if (!this.hasShownTabBarRedDot) {
          uni.showTabBarRedDot({
            index: 1
          })
        } else {
          uni.hideTabBarRedDot({
            index: 1
          })
        }
        this.hasShownTabBarRedDot = !this.hasShownTabBarRedDot
      },
      hideTabBar() {
        if (!this.hasHiddenTabBar) {
          uni.hideTabBar()
        } else {
          uni.showTabBar()
        }
        this.hasHiddenTabBar = !this.hasHiddenTabBar
      },
      customStyle() {
        if (this.hasCustomedStyle) {
          uni.setTabBarStyle({
            color: '#7A7E83',
            selectedColor: '#007AFF',
            backgroundColor: '#F8F8F8',
115 116
            borderStyle: 'black',
            // 新增 borderColor,优先级高于 borderStyle
117
            // borderColor:'red'
118 119 120 121 122 123
          })
        } else {
          uni.setTabBarStyle({
            color: '#FFF',
            selectedColor: '#007AFF',
            backgroundColor: '#000000',
124
            borderStyle: 'black',
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
          })
        }
        this.hasCustomedStyle = !this.hasCustomedStyle
      },
      customItem() {
        let tabBarOptions = {
          index: 1,
          text: '接口',
          iconPath: '/static/api.png',
          selectedIconPath: '/static/apiHL.png'
        } as SetTabBarItemOptions
        if (this.hasCustomedItem) {
          uni.setTabBarItem(tabBarOptions)
        } else {
          tabBarOptions.text = 'API'
          uni.setTabBarItem(tabBarOptions)
        }
        this.hasCustomedItem = !this.hasCustomedItem
      }
    }
  }
W
wanganxp 已提交
146 147 148
</script>

<style>
149 150 151 152 153
  .button {
    margin-top: 15px;
    margin-left: 0;
    margin-right: 0;
  }
W
wanganxp 已提交
154

155 156 157
  .btn-area {
    padding-top: 15px;
  }
W
wanganxp 已提交
158
</style>