api-set-tabbar.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // #ifdef APP-ANDROID || APP-IOS || WEB || APP-HARMONY
  10. <button class="button" @click="hideTabBarItem">{{ !hasHiddenTabBarItem ? '隐藏接口Item' : '显示接口Item' }}</button>
  11. // #endif
  12. <button class="button" @click="setTabBarTitle">{{ !hasSetLongTitle ? '自定义超长标题' : '移除自定义信息' }}</button>
  13. <view class="btn-area">
  14. <!-- <button class="button" type="primary" @click="navigateBack">关闭</button> -->
  15. </view>
  16. </view>
  17. </template>
  18. <script lang="uts">
  19. export default {
  20. data() {
  21. return {
  22. title: 'tababr',
  23. hasSetTabBarBadge: false,
  24. hasShownTabBarRedDot: false,
  25. hasCustomedStyle: false,
  26. hasCustomedItem: false,
  27. hasHiddenTabBar: false,
  28. hasHiddenTabBarItem: false,
  29. hasSetLongTitle: false,
  30. }
  31. },
  32. destroyed() {
  33. if (this.hasSetTabBarBadge) {
  34. uni.removeTabBarBadge({
  35. index: 1
  36. })
  37. }
  38. if (this.hasShownTabBarRedDot) {
  39. uni.hideTabBarRedDot({
  40. index: 1
  41. })
  42. }
  43. if (this.hasHiddenTabBar) {
  44. uni.showTabBar()
  45. }
  46. if (this.hasCustomedStyle) {
  47. uni.setTabBarStyle({
  48. color: '#7A7E83',
  49. selectedColor: '#007AFF',
  50. backgroundColor: '#F8F8F8',
  51. borderStyle: 'black'
  52. })
  53. }
  54. if (this.hasCustomedItem) {
  55. let tabBarOptions = {
  56. index: 1,
  57. text: '接口',
  58. iconPath: '/static/api.png',
  59. selectedIconPath: '/static/apiHL.png'
  60. } as SetTabBarItemOptions
  61. uni.setTabBarItem(tabBarOptions)
  62. }
  63. if (this.hasHiddenTabBarItem || this.hasSetLongTitle) {
  64. let tabBarOptions = {
  65. visible: true,
  66. index: 1,
  67. text: '接口',
  68. iconPath: '/static/api.png',
  69. selectedIconPath: '/static/apiHL.png'
  70. } as SetTabBarItemOptions
  71. uni.setTabBarItem(tabBarOptions)
  72. }
  73. },
  74. methods: {
  75. setTabBarTitle(){
  76. // #ifdef APP-HARMONY
  77. uni.showToast({
  78. title: "暂不支持"
  79. })
  80. // #endif
  81. // #ifndef APP-HARMONY
  82. let tabBarOptions = {
  83. visible: true,
  84. index: 1,
  85. text: '接口',
  86. iconPath: '/static/api.png',
  87. selectedIconPath: '/static/apiHL.png'
  88. } as SetTabBarItemOptions
  89. if (!this.hasSetLongTitle) {
  90. tabBarOptions.text = "超长标题内容超长标题内容超长标题内容超长标题测试";
  91. tabBarOptions.iconPath = "";
  92. tabBarOptions.selectedIconPath = "";
  93. } else {
  94. tabBarOptions.text = "接口";
  95. tabBarOptions.iconPath = "/static/api.png";
  96. tabBarOptions.selectedIconPath = "/static/apiHL.png";
  97. }
  98. uni.setTabBarItem(tabBarOptions)
  99. this.hasSetLongTitle = !this.hasSetLongTitle
  100. // #endif
  101. },
  102. hideTabBarItem(){
  103. // #ifdef APP-HARMONY
  104. uni.showToast({
  105. title: "暂不支持"
  106. })
  107. // #endif
  108. // #ifndef APP-HARMONY
  109. let tabBarOptions = {
  110. visible: true,
  111. index: 1,
  112. text: '接口',
  113. iconPath: '/static/api.png',
  114. selectedIconPath: '/static/apiHL.png'
  115. } as SetTabBarItemOptions
  116. if (!this.hasHiddenTabBarItem) {
  117. tabBarOptions.visible = false;
  118. } else {
  119. tabBarOptions.visible = true;
  120. }
  121. uni.setTabBarItem(tabBarOptions)
  122. this.hasHiddenTabBarItem = !this.hasHiddenTabBarItem
  123. // #endif
  124. },
  125. navigateBack() {
  126. this.$emit('unmount')
  127. },
  128. setTabBarBadge() {
  129. if (this.hasShownTabBarRedDot) {
  130. uni.hideTabBarRedDot({
  131. index: 1
  132. })
  133. this.hasShownTabBarRedDot = !this.hasShownTabBarRedDot
  134. }
  135. if (!this.hasSetTabBarBadge) {
  136. uni.setTabBarBadge({
  137. index: 1,
  138. text: '1'
  139. })
  140. } else {
  141. uni.removeTabBarBadge({
  142. index: 1
  143. })
  144. }
  145. this.hasSetTabBarBadge = !this.hasSetTabBarBadge
  146. },
  147. showTabBarRedDot() {
  148. if (this.hasSetTabBarBadge) {
  149. uni.removeTabBarBadge({
  150. index: 1
  151. })
  152. this.hasSetTabBarBadge = !this.hasSetTabBarBadge
  153. }
  154. if (!this.hasShownTabBarRedDot) {
  155. uni.showTabBarRedDot({
  156. index: 1
  157. })
  158. } else {
  159. uni.hideTabBarRedDot({
  160. index: 1
  161. })
  162. }
  163. this.hasShownTabBarRedDot = !this.hasShownTabBarRedDot
  164. },
  165. hideTabBar() {
  166. if (!this.hasHiddenTabBar) {
  167. uni.hideTabBar()
  168. } else {
  169. uni.showTabBar()
  170. }
  171. this.hasHiddenTabBar = !this.hasHiddenTabBar
  172. },
  173. customStyle() {
  174. if (this.hasCustomedStyle) {
  175. uni.setTabBarStyle({
  176. color: '#7A7E83',
  177. selectedColor: '#007AFF',
  178. backgroundColor: '#F8F8F8',
  179. borderStyle: 'black',
  180. // 新增 borderColor,优先级高于 borderStyle
  181. // borderColor:'red'
  182. })
  183. } else {
  184. uni.setTabBarStyle({
  185. color: '#FFF',
  186. selectedColor: '#007AFF',
  187. backgroundColor: '#000000',
  188. borderStyle: 'black',
  189. })
  190. }
  191. this.hasCustomedStyle = !this.hasCustomedStyle
  192. },
  193. customItem() {
  194. // #ifdef APP-HARMONY
  195. uni.showToast({
  196. title: "暂不支持"
  197. })
  198. // #endif
  199. // #ifndef APP-HARMONY
  200. let tabBarOptions = {
  201. index: 1,
  202. text: '接口',
  203. iconPath: '/static/api.png',
  204. selectedIconPath: '/static/apiHL.png'
  205. } as SetTabBarItemOptions
  206. if (this.hasCustomedItem) {
  207. uni.setTabBarItem(tabBarOptions)
  208. } else {
  209. tabBarOptions.text = 'API'
  210. uni.setTabBarItem(tabBarOptions)
  211. }
  212. this.hasCustomedItem = !this.hasCustomedItem
  213. // #endif
  214. }
  215. }
  216. }
  217. </script>
  218. <style>
  219. .button {
  220. margin-top: 15px;
  221. margin-left: 0;
  222. margin-right: 0;
  223. }
  224. .btn-area {
  225. padding-top: 15px;
  226. }
  227. </style>