当前位置:   article > 正文

uni-app 实现根据用户不同角色显示不同的tabBar_u-tabbar

u-tabbar

在网上大量百度之后,好多都走不通,自己总结了一下!!!

我的方法原理:  相当于封装一个公共组件,需要用的地方就调用,(把自带的tabBar隐藏了就好了),

样子都一样的,无非就是数据不一样,千万别想太复杂了

 

创建 组件 tabBar,

  1. <template>
  2. <view>
  3. <view class="tabBar_css">
  4. <u-tabbar
  5. :value="currentPagePath"
  6. :fixed="true"
  7. :placeholder="true"
  8. :safeAreaInsetBottom="true"
  9. >
  10. <u-tabbar-item
  11. v-for="(item,index) in tabBerList"
  12. :key="index"
  13. :text="item.text"
  14. :icon="item.iconPath"
  15. :name="item.pagePath"
  16. @click="click_page"
  17. >
  18. </u-tabbar-item>
  19. </u-tabbar>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import {
  25. mapGetters
  26. } from 'vuex'
  27. export default {
  28. data() {
  29. return {
  30. };
  31. },
  32. props: {
  33. currentPagePath: String,
  34. },
  35. computed: {
  36. ...mapGetters(['tabBerList'])
  37. },
  38. methods: {
  39. click_page(arg) {
  40. console.log('arg',arg);
  41. let page = '/' + arg
  42. uni.switchTab({
  43. url: page,
  44. success: function(res) {
  45. console.log(res);
  46. console.log('succcess');
  47. },
  48. fail: function(res) {
  49. console.log('fail');
  50. console.log(res);
  51. }
  52. })
  53. }
  54. }
  55. }
  56. </script>
  57. <style lang="scss">
  58. .tabBar_css {
  59. position: fixed;
  60. bottom: 0;
  61. background-color: red;
  62. z-index: 9999;
  63. width: 100%;
  64. }
  65. </style>

创建组件xiaoz.vue,根据权限(角色)调用不同的数据

  1. <template>
  2. <view class="content">
  3. <view v-if="ccc=='0'">
  4. <u-tabbar
  5. :value="value1"
  6. @change="change1"
  7. :fixed="false"
  8. :placeholder="false"
  9. :safeAreaInsetBottom="false"
  10. >
  11. <u-tabbar-item v-for="(item,index) in tabBerList1" ref="div1" :text="item.text" :icon="bbb == index? item.selectedIconPath : item.iconPath" @click="click1(index,item.pagePath)" ></u-tabbar-item>
  12. </u-tabbar>
  13. </view>
  14. <view v-if="ccc=='1'">
  15. <u-tabbar
  16. :value="value1"
  17. @change="change1"
  18. >
  19. <!-- :fixed="false"
  20. :placeholder="false"
  21. :safeAreaInsetBottom="false" -->
  22. <u-tabbar-item v-for="(item,index) in tabBerList0" :text="item.text" :icon="bbb == index? item.selectedIconPath : item.iconPath" @click="click1(index,item.pagePath)" ></u-tabbar-item>
  23. </u-tabbar>
  24. </view>
  25. </view>
  26. <!-- 111 -->
  27. </template>
  28. <script>
  29. import { mapGetters } from 'vuex'
  30. export default {
  31. data() {
  32. return {
  33. // borderTop: false,
  34. // inactiveColor: '#909399',
  35. // activeColor: '#5098FF',
  36. value1:"",
  37. ccc:uni.getStorageSync('id'),
  38. bbb:uni.getStorageSync('xx')
  39. }
  40. },
  41. onShow() {
  42. //隐藏官方的tabBar
  43. },
  44. methods:{
  45. click1(a,b){
  46. // console.log('a',a);
  47. uni.setStorageSync('xx',a)
  48. // console.log('bbb',this.bbb);
  49. uni.switchTab({
  50. url:"/"+b,
  51. })
  52. // if(this.ccc=='0'){
  53. // uni.switchTab({
  54. // url:"/"+b,
  55. // })
  56. // }else{
  57. // uni.redirectTo({
  58. // url:"/"+b,
  59. // })
  60. // }
  61. },
  62. change1(a,b){
  63. // this.bbb=a
  64. // console.log(a,b);
  65. }
  66. },
  67. computed: {
  68. ...mapGetters([
  69. 'tabBerList0',
  70. 'tabBerList1'
  71. // 'midBtn'
  72. ])
  73. }
  74. }
  75. </script>
  76. <style scoped lang="scss">
  77. .content{
  78. position: fixed;
  79. bottom: 0rpx;
  80. width:100%
  81. }
  82. </style>

下面的就是创建个vuex,放进去了3组数据(因为有3个角色),网上的是真麻烦啊(我也是抄的)!!!

 

tabBer.js

  1. import tabBer from '@/untils/tabBar.js'
  2. // tabBar文件为我们创建的tabBer对象数组
  3. // 判断用户tabBer类别
  4. // 0 冻结
  5. // 1 普通用户
  6. // 2 教师
  7. // 3 管理员
  8. // 逻辑判断处理
  9. // let xx=uni.getStorageSync('id')
  10. // let type = xx==='2' ? 'schList' : xx==='1' ? 'tchList' : 'stuList'
  11. // // let type='schList'
  12. // console.log(xx);
  13. // console.log(type);
  14. // midBtn 为设置tabBer中间的凸起,false为不凸起
  15. const state = {
  16. // list: tabBer[type],
  17. list1: tabBer['schList'],
  18. list0: tabBer['stuList'],
  19. data:0
  20. // midBtn: type === 'stuList' ? false : true
  21. }
  22. export default {
  23. namespaced: true,
  24. state
  25. }

getters.js

  1. const getters = {
  2. tabBerList1: state => state.tabBer.list1,
  3. tabBerList0: state => state.tabBer.list0,
  4. data:state=>state.tabBer.data
  5. // midBtn: state => state.tabBer.midBtn
  6. }
  7. export default getters

index.js

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import tabBer from './modules/tabBer'
  4. import getters from './getters'
  5. Vue.use(Vuex)
  6. const store = new Vuex.Store({
  7. modules: {
  8. tabBer
  9. },
  10. getters
  11. })
  12. export default store

 untils文件  tabBar.js

  1. const stuList = [
  2. {
  3. pagePath:"pages/gysshow/gysshow",
  4. iconPath: "../../static/imgs/tab1.png",
  5. selectedIconPath: "../../static/imgs/tab2.png",
  6. text: "首页",
  7. },
  8. {
  9. pagePath:"pages/xxshow/xxshow",
  10. iconPath: "../../static/imgs/tab3.png",
  11. selectedIconPath: "../../static/imgs/tab4.png",
  12. text: "品类",
  13. },
  14. {
  15. pagePath:"pages/my1/my1",
  16. iconPath: "../../static/imgs/tab5.png",
  17. selectedIconPath: "../../static/imgs/tab6.png",
  18. text: "我的",
  19. }
  20. ];
  21. const schList = [
  22. {
  23. pagePath:"pages/index/index",
  24. iconPath: "../../static/imgs/tab1.png",
  25. selectedIconPath: "../../static/imgs/tab2.png",
  26. text: "首页",
  27. },
  28. {
  29. pagePath:"pages/health/health",
  30. iconPath: "../../static/imgs/tab3.png",
  31. selectedIconPath: "../../static/imgs/tab4.png",
  32. text: "品类",
  33. },
  34. {
  35. pagePath:"pages/my/my",
  36. iconPath: "../../static/imgs/tab5.png",
  37. selectedIconPath: "../../static/imgs/tab6.png",
  38. text: "我的",
  39. }
  40. ];
  41. export default {
  42. stuList,
  43. schList
  44. };

pages.json 里面tabBar 配置

微信的tabBar最多5个,所以不能折磨写, 但其他的好像能折磨用(原因是因为这样用可以解决闪烁问题)

  1. onShow: function() {
  2. // console.log(this)
  3. uni.hideTabBar()
  4. },

切换tabBar时,需要把这个隐藏了,不然会报错

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/113086
推荐阅读
相关标签
  

闽ICP备14008679号