api-set-tabbar.vue 3.2 KB

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