api-set-tabbar.nvue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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) {
  28. uni.removeTabBarBadge({
  29. index: 1
  30. })
  31. }
  32. if (this.hasShownTabBarRedDot) {
  33. uni.hideTabBarRedDot({
  34. index: 1
  35. })
  36. }
  37. if (this.hasHiddenTabBar) {
  38. uni.showTabBar()
  39. }
  40. if (this.hasCustomedStyle) {
  41. uni.setTabBarStyle({
  42. color: '#7A7E83',
  43. selectedColor: '#007AFF',
  44. backgroundColor: '#F8F8F8',
  45. borderStyle: 'black'
  46. })
  47. }
  48. if (this.hasCustomedItem) {
  49. let tabBarOptions = {
  50. index: 1,
  51. text: '接口',
  52. iconPath: '/static/api.png',
  53. selectedIconPath: '/static/apiHL.png'
  54. }
  55. uni.setTabBarItem(tabBarOptions)
  56. }
  57. },
  58. methods: {
  59. navigateBack() {
  60. this.$emit('unmount')
  61. },
  62. setTabBarBadge() {
  63. if(this.hasShownTabBarRedDot){
  64. uni.hideTabBarRedDot({
  65. index: 1
  66. })
  67. this.hasShownTabBarRedDot = !this.hasShownTabBarRedDot
  68. }
  69. if (!this.hasSetTabBarBadge) {
  70. uni.setTabBarBadge({
  71. index: 1,
  72. text: '1'
  73. })
  74. } else {
  75. uni.removeTabBarBadge({
  76. index: 1
  77. })
  78. }
  79. this.hasSetTabBarBadge = !this.hasSetTabBarBadge
  80. },
  81. showTabBarRedDot() {
  82. if(this.hasSetTabBarBadge) {
  83. uni.removeTabBarBadge({
  84. index: 1
  85. })
  86. this.hasSetTabBarBadge = !this.hasSetTabBarBadge
  87. }
  88. if (!this.hasShownTabBarRedDot) {
  89. uni.showTabBarRedDot({
  90. index: 1
  91. })
  92. } else {
  93. uni.hideTabBarRedDot({
  94. index: 1
  95. })
  96. }
  97. this.hasShownTabBarRedDot = !this.hasShownTabBarRedDot
  98. },
  99. hideTabBar() {
  100. if (!this.hasHiddenTabBar) {
  101. uni.hideTabBar()
  102. } else {
  103. uni.showTabBar()
  104. }
  105. this.hasHiddenTabBar = !this.hasHiddenTabBar
  106. },
  107. customStyle() {
  108. if (this.hasCustomedStyle) {
  109. uni.setTabBarStyle({
  110. color: '#7A7E83',
  111. selectedColor: '#007AFF',
  112. backgroundColor: '#F8F8F8',
  113. borderStyle: 'black'
  114. })
  115. } else {
  116. uni.setTabBarStyle({
  117. color: '#FFF',
  118. selectedColor: '#007AFF',
  119. backgroundColor: '#000000',
  120. borderStyle: 'black'
  121. })
  122. }
  123. this.hasCustomedStyle = !this.hasCustomedStyle
  124. },
  125. customItem() {
  126. let tabBarOptions = {
  127. index: 1,
  128. text: '接口',
  129. iconPath: '/static/api.png',
  130. selectedIconPath: '/static/apiHL.png'
  131. }
  132. if (this.hasCustomedItem) {
  133. uni.setTabBarItem(tabBarOptions)
  134. } else {
  135. tabBarOptions.text = 'API'
  136. uni.setTabBarItem(tabBarOptions)
  137. }
  138. this.hasCustomedItem = !this.hasCustomedItem
  139. }
  140. }
  141. }
  142. </script>
  143. <style>
  144. .button {
  145. margin-top: 30rpx;
  146. margin-left: 0;
  147. margin-right: 0;
  148. }
  149. .btn-area {
  150. padding-top: 30rpx;
  151. }
  152. </style>