api-set-tabbar.nvue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <view class="uni-padding-wrap">
  3. <page-head :title="title"></page-head>
  4. <button class="button" @click="setTabBarBadge">{{ !hasSetTabBarBadge ? '设置tab徽标' : '移除tab徽标' }}</button>
  5. <button class="button" @click="showTabBarRedDot">{{ !hasShownTabBarRedDot ? '显示红点' : '移除红点'}}</button>
  6. <button class="button" @click="customStyle">{{ !hasCustomedStyle ? '自定义Tab样式' : '移除自定义样式'}}</button>
  7. <button class="button" @click="customItem">{{ !hasCustomedItem ? '自定义Tab信息' : '移除自定义信息' }}</button>
  8. <button class="button" @click="hideTabBar">{{ !hasHiddenTabBar ? '隐藏TabBar' : '显示TabBar' }}</button>
  9. <view class="btn-area">
  10. <button class="button" type="primary" @click="navigateBack">返回上一级</button>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. data() {
  17. return {
  18. title: 'tababr',
  19. hasSetTabBarBadge: false,
  20. hasShownTabBarRedDot: false,
  21. hasCustomedStyle: false,
  22. hasCustomedItem: false,
  23. hasHiddenTabBar: false
  24. }
  25. },
  26. destroyed() {
  27. if (this.hasSetTabBarBadge) { uni.removeTabBarBadge({ index: 1 }) }
  28. if (this.hasShownTabBarRedDot) { uni.hideTabBarRedDot({ index: 1 }) }
  29. if (this.hasHiddenTabBar) { uni.showTabBar() }
  30. if (this.hasCustomedStyle) {
  31. uni.setTabBarStyle({ color: '#7A7E83', selectedColor: '#007AFF', backgroundColor: '#F8F8F8', borderStyle: 'black' })
  32. }
  33. if (this.hasCustomedItem) {
  34. let tabBarOptions = {
  35. index: 1,
  36. text: '接口',
  37. iconPath: '/static/api.png',
  38. selectedIconPath: '/static/apiHL.png'
  39. }
  40. uni.setTabBarItem(tabBarOptions)
  41. }
  42. },
  43. methods: {
  44. navigateBack() {
  45. this.$emit('unmount')
  46. },
  47. setTabBarBadge() {
  48. if(this.hasShownTabBarRedDot){
  49. uni.hideTabBarRedDot({ index: 1 })
  50. this.hasShownTabBarRedDot = !this.hasShownTabBarRedDot
  51. }
  52. if (!this.hasSetTabBarBadge) {
  53. uni.setTabBarBadge({
  54. index: 1,
  55. text: '1'
  56. })
  57. } else {
  58. uni.removeTabBarBadge({
  59. index: 1
  60. })
  61. }
  62. this.hasSetTabBarBadge = !this.hasSetTabBarBadge
  63. },
  64. showTabBarRedDot() {
  65. if(this.hasSetTabBarBadge) {
  66. uni.removeTabBarBadge({
  67. index: 1
  68. })
  69. this.hasSetTabBarBadge = !this.hasSetTabBarBadge
  70. }
  71. if (!this.hasShownTabBarRedDot) {
  72. uni.showTabBarRedDot({
  73. index: 1
  74. })
  75. } else {
  76. uni.hideTabBarRedDot({
  77. index: 1
  78. })
  79. }
  80. this.hasShownTabBarRedDot = !this.hasShownTabBarRedDot
  81. },
  82. hideTabBar() {
  83. if (!this.hasHiddenTabBar) {
  84. uni.hideTabBar()
  85. } else {
  86. uni.showTabBar()
  87. }
  88. this.hasHiddenTabBar = !this.hasHiddenTabBar
  89. },
  90. customStyle() {
  91. if (this.hasCustomedStyle) {
  92. uni.setTabBarStyle({
  93. color: '#7A7E83',
  94. selectedColor: '#007AFF',
  95. backgroundColor: '#F8F8F8',
  96. borderStyle: 'black'
  97. })
  98. } else {
  99. uni.setTabBarStyle({
  100. color: '#FFF',
  101. selectedColor: '#007AFF',
  102. backgroundColor: '#000000',
  103. borderStyle: 'black'
  104. })
  105. }
  106. this.hasCustomedStyle = !this.hasCustomedStyle
  107. },
  108. customItem() {
  109. let tabBarOptions = {
  110. index: 1,
  111. text: '接口',
  112. iconPath: '/static/api.png',
  113. selectedIconPath: '/static/apiHL.png'
  114. }
  115. if (this.hasCustomedItem) {
  116. uni.setTabBarItem(tabBarOptions)
  117. } else {
  118. tabBarOptions.text = 'API'
  119. uni.setTabBarItem(tabBarOptions)
  120. }
  121. this.hasCustomedItem = !this.hasCustomedItem
  122. }
  123. }
  124. }
  125. </script>
  126. <style>
  127. .button {
  128. margin-top: 30rpx;
  129. margin-left: 0;
  130. margin-right: 0;
  131. }
  132. .btn-area {
  133. padding-top: 30rpx;
  134. }
  135. </style>