Uniapp根据权限(角色)不同动态展示底部tabbar

比如绑定openId展示的tabbar为:首页、巡检、工单

未绑定openId展示的tabbar为:在线报修、我的报修

首页配置pages.json中的tabbar:

这里只用配置pagePath就可以了~

具体代码如下:

 "tabBar": {
    "custom": true,
    "color": "#bfbfbf",
    "selectedColor": "#226be4", // 选中tab栏文本颜色
    "list": [{
        "pagePath": "pages/home/home"
        // "text": "首页",
        // "iconPath": "static/shouye.png",
        // "selectedIconPath": "static/shouye(1).png"
      },
      {
        "pagePath": "pages/plan/plan"
        // "text": "巡检",
        // "iconPath": "static/xunjian.png",
        // "selectedIconPath": "static/xunjian (1).png"
      },
      {
        "pagePath": "pages/tickets/tickets"
        // "text": "工单",
        // "iconPath": "static/tickets.png",
        // "selectedIconPath": "static/tickets(1).png"
      },
      {
        "pagePath": "pages/onlrepairs/onlrepairs"
        // "text": "在线报修",
        // "iconPath": "static/onlrepair.png",
        // "selectedIconPath": "static/onlrepair(1).png"
      },
      {
        "pagePath": "pages/my/my"
        // "text": "我的报修",
        // "iconPath": "static/baoxiu.png",
        // "selectedIconPath": "static/baoxiu (1).png"
      }
    ]
  }

创建一个自定义的tabbar文件:

具体代码如下:

注意:pagePath的最前面要  加  /

// 未绑定openId展示的页面
const publicBar = [{
    "pagePath": "/pages/onlrepairs/onlrepairs",
    "text": "在线报修",
    "iconPath": "/static/onlrepair.png",
    "selectedIconPath": "/static/onlrepair(1).png"
  },
  {
    "pagePath": "/pages/my/my",
    "text": "我的报修",
    "iconPath": "/static/baoxiu.png",
    "selectedIconPath": "/static/baoxiu (1).png"
  }
]

// 绑定openId展示的页面
const selfBar = [{
    "pagePath": "/pages/home/home",
    "text": "首页",
    "iconPath": "/static/shouye.png",
    "selectedIconPath": "/static/shouye(1).png"
  },
  {
    "pagePath": "/pages/plan/plan",
    "text": "巡检",
    "iconPath": "/static/xunjian.png",
    "selectedIconPath": "/static/xunjian (1).png"
  },
  {
    "pagePath": "/pages/tickets/tickets",
    "text": "工单",
    "iconPath": "/static/tickets.png",
    "selectedIconPath": "/static/tickets(1).png"
  }
]

export {
  publicBar,
  selfBar
}

创建index.js文件配置vuex  :

具体代码如下:

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		dynamicTabbar: [] // 动态tabbar
	},
	getters: {},
	actions: {
		changeTabbar({commit}, payload) {
			commit('updateTabbar', payload)
		}
	},
	mutations: {
		updateTabbar(state, payload) {
			state.dynamicTabbar = payload
		}
	}
})

export default store

Vuex简易了解:

  1. state: 统一定义公共数据(类似于data(){return {a:1, b:2,xxxxxx}})
  2. mutations : 使用它来修改数据(类似于methods)更改state中的数据必须使用mutations来进行更改
  3. getters: 类似于computed(计算属性,对现有的状态进行计算得到新的数据-------派生 )
  4. actions: 发起异步请求
  5. modules: 模块拆分

在main.js中引入并挂载store:

在Login页面引入自定义tabbar页面并判断:

最后在每个页面使用动态的tabbar:

具体代码如下:

 <!-- 动态渲染tabBar -->
    <u-tabbar v-model="current" :list="tabBarList" :active-color="activeColor" :inactive-color="inactiveColor"
      :border-top="borderTop" />

  data() {
      return {
         title: '首页',
         tabBarList: this.$store.state.dynamicTabbar,
         current: 0,
         borderTop: true,
         inactiveColor: '#909399',
         activeColor: '#5098FF',
      }
  }
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以通过以下步骤来实现uniapp自定义tabbar,实现不同角色进入显示不同底部tabBar: 1. 在pages文件夹下创建对应的角色页面,例如:student、teacher、admin等。 2. 在每个角色页面的vue文件中,使用自定义底部tabBar组件,并且通过props向组件传递当前角色的标识。 3. 在自定义底部tabBar组件中,根据接收到的角色标识动态渲染对应的底部tabBar。 4. 在App.vue文件中,通过uni.showTabBaruni.hideTabBar方法来显示或隐藏底部tabBar。 5. 在main.js文件中,通过uni.getStorageSync方法获取当前用户角色信息,并将其存储在全局变量中,以便在各个页面中使用。 6. 在页面跳转时,需要根据当前用户角色信息来判断是否需要显示底部tabBar,并且通过uni.redirectTo或uni.navigateTo方法实现页面跳转。 举个例子,如果当前用户是学生,则在student页面中使用以下代码来引入自定义底部tabBar组件: ``` <template> <view> <text>这是学生页面</text> <my-tab-bar :role="'student'" /> </view> </template> <script> import MyTabBar from '@/components/MyTabBar.vue' export default { components: { MyTabBar } } </script> ``` 在自定义底部tabBar组件中,可以使用以下代码来根据接收到的角色标识动态渲染对应的底部tabBar: ``` <template> <view> <view v-if="role === 'student'"> <!-- 学生底部tabBar --> </view> <view v-else-if="role === 'teacher'"> <!-- 教师底部tabBar --> </view> <view v-else-if="role === 'admin'"> <!-- 管理员底部tabBar --> </view> </view> </template> <script> export default { props: { role: { type: String, required: true } } } </script> ``` 通过以上步骤,就可以实现uniapp自定义tabbar,实现不同角色进入显示不同底部tabBar的功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值