当前位置:   article > 正文

uni.setTabBarItem动态切换导航栏

uni.settabbaritem

登录时,需要根据不同的角色进入不同的主页,并且显示不同的导航栏,根据DCloud官网的API介绍,使用`uni.setTabBarItem`可以动态切换导航栏,但是其中的`pagePath`,`text`等关键参数修改均不生效,`iconPath`参数可以修改,但不重要,无法实现跳转页面的动态切换,于是转换思路,使用`visible`属性来实现动态切换的效果。

首先,需要在`pages.json`中定义所有导航页面,注意,是所有的,包含不同用户的导航页面

例如,我定义了10个导航按钮,前5个显示(不填`visible`参数时默认为`true`),后5个页面隐藏(`visible = false`)

  1. "tabBar": {
  2. "borderStyle": "black",
  3. "backgroundColor": "#ffffff",
  4. "color": "#7A7E83",
  5. "selectedColor": "#0871E3",
  6. "iconfontSrc": "/static/fonts/iconfont.ttf",
  7. "list": [
  8. {
  9. "pagePath": "pages/index/index",
  10. "text": "%page.index1%",
  11. "iconfont": {
  12. "text": "\ue6a6",
  13. "selectedText": "\ue6a6",
  14. "fontSize": "22px",
  15. "color": "#7A7E83",
  16. "selectedColor": "#0871E3"
  17. }
  18. },
  19. {
  20. "pagePath": "pages/distributionManagement/distributionManagement",
  21. "text": "%page.index2%",
  22. "iconfont": {
  23. "text": "\ue60a",
  24. "selectedText": "\ue60a",
  25. "fontSize": "22px",
  26. "color": "#7A7E83",
  27. "selectedColor": "#0871E3"
  28. }
  29. },
  30. {
  31. "pagePath": "pages/report/report",
  32. "text": "%page.index3%",
  33. "iconfont": {
  34. "text": "\ue61e",
  35. "selectedText": "\ue61e",
  36. "fontSize": "22px",
  37. "color": "#7A7E83",
  38. "selectedColor": "#0871E3"
  39. }
  40. },
  41. {
  42. "pagePath": "pages/customerManage/customerManage",
  43. "text": "%page.index4%",
  44. "iconfont": {
  45. "text": "\ue639",
  46. "selectedText": "\ue639",
  47. "fontSize": "22px",
  48. "color": "#7A7E83",
  49. "selectedColor": "#0871E3"
  50. }
  51. },
  52. {
  53. "pagePath": "pages/mine/mine",
  54. "text": "%page.index5%",
  55. "iconfont": {
  56. "text": "\ue608",
  57. "selectedText": "\ue608",
  58. "fontSize": "22px",
  59. "color": "#7A7E83",
  60. "selectedColor": "#0871E3"
  61. }
  62. },
  63. {
  64. "pagePath": "pages/foreign/index/index",
  65. "text": "%page.index1%",
  66. "iconfont": {
  67. "text": "\ue6a6",
  68. "selectedText": "\ue6a6",
  69. "fontSize": "22px",
  70. "color": "#7A7E83",
  71. "selectedColor": "#0871E3"
  72. },
  73. "visible": false
  74. },
  75. {
  76. "pagePath": "pages/foreign/distributionManagement/distributionManagement",
  77. "text": "%page.index2%",
  78. "iconfont": {
  79. "text": "\ue60a",
  80. "selectedText": "\ue60a",
  81. "fontSize": "22px",
  82. "color": "#7A7E83",
  83. "selectedColor": "#0871E3"
  84. },
  85. "visible": false
  86. },
  87. {
  88. "pagePath": "pages/foreign/report/report",
  89. "text": "%page.index3%",
  90. "iconfont": {
  91. "text": "\ue61e",
  92. "selectedText": "\ue61e",
  93. "fontSize": "22px",
  94. "color": "#7A7E83",
  95. "selectedColor": "#0871E3"
  96. },
  97. "visible": false
  98. },
  99. {
  100. "pagePath": "pages/foreign/customerManage/customerManage",
  101. "text": "%page.index4%",
  102. "iconfont": {
  103. "text": "\ue639",
  104. "selectedText": "\ue639",
  105. "fontSize": "22px",
  106. "color": "#7A7E83",
  107. "selectedColor": "#0871E3"
  108. },
  109. "visible": false
  110. },
  111. {
  112. "pagePath": "pages/foreign/mine/mine",
  113. "text": "%page.index5%",
  114. "iconfont": {
  115. "text": "\ue608",
  116. "selectedText": "\ue608",
  117. "fontSize": "22px",
  118. "color": "#7A7E83",
  119. "selectedColor": "#0871E3"
  120. },
  121. "visible": false
  122. }
  123. ]
  124. }

在登录时,提交到后台校验,并判断该用户的系统角色,例如我的系统中,有「普通用户」和「外商用户」两种角色,登录成功后将信息返回到前端。

例如,前端判断到这个用户是「外商用户」,那么循环10次(因为总共有10个导航页面),将前5个导航页面隐藏,后5个导航页面显示:

  1. login() {
  2. let data = {} // 发送请求到后端校验登录信息得到data
  3. let isForeign = data.isForeign // 是否是外商用户
  4. // 如果是外商,隐藏普通用户页面,显示外商页面
  5. if (isForeign) {
  6. // 隐藏前5个导航,显示后5个导航页面
  7. for (let i = 0; i < 10; i++) {
  8. let visible = true
  9. if (i < 5) {
  10. visible = false
  11. }
  12. uni.setTabBarItem({
  13. index: i,
  14. visible: visible
  15. })
  16. }
  17. // 跳转外商用户首页页面
  18. uni.switchTab({
  19. url: '/pages/foreign/index/index'
  20. });
  21. } else {
  22. // 跳转普通用户首页页面
  23. uni.switchTab({
  24. url: '/pages/index/index'
  25. });
  26. }
  27. }

 ⚠️请根据自己的系统进行相应的修改

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号