uniapp 根据不同角色实现动态底部TabBar导航栏

本文介绍了如何在UniApp项目中使用uView-UI框架实现根据不同用户角色动态配置TabBar的功能,包括配置page.json、创建自定义tabBar文件、集成Vuex进行状态管理以及在登录验证后根据角色切换TabBar内容。
摘要由CSDN通过智能技术生成


前言

在UniApp的开发过程中,为了针对不同角色用户登录后的个性化需求,本文集成了uView-UI框架的TabBar组件。通过动态权限配置机制,能够根据用户的角色信息灵活地调整TabBar的属性,从而实现个性化的TabBar界面展示,以满足不同用户群体的特定需求。


最终效果

用户角色:
在这里插入图片描述

售后客服:
在这里插入图片描述


一、实现步骤

1.配置page.json中的tabBar属性

代码如下:

只需配置pagePath,微信小程序底部导航栏最多只能显示五个。

在这里插入图片描述

2.创建自定义tabBar文件

位置示例如下:

在这里插入图片描述

具体代码如下:

在tabBar.js文件中定义的list数组的顺序决定了tabBar上按钮的显示顺序。

// 普通用户展示的页面
const publicBar = [{
		"pagePath": "/pages/sys/home/index",
		"text": "首页",
		"iconPath": "/static/aidex/tabbar/home_1.png",
		"selectedIconPath": "/static/aidex/tabbar/home_2.png"
	},
	{
		"pagePath": "/pages/sys/msg/index",
		"text": "消息",
		"iconPath": "/static/aidex/tabbar/msg_1.png",
		"selectedIconPath": "/static/aidex/tabbar/msg_2.png"
	},
	{
		"pagePath": "/pages/sys/aftersales/aftersales-order",
		"text": "我的售后",
		"iconPath": "/static/aidex/tabbar/book_1.png",
		"selectedIconPath": "/static/aidex/tabbar/book_2.png"
	},
	{
		"pagePath": "/pages/sys/user/index",
		"text": "我的",
		"iconPath": "/static/aidex/tabbar/my_1.png",
		"selectedIconPath": "/static/aidex/tabbar/my_2.png",
	}
]

// 售后客服展示的页面
const selfBar = [{
		"pagePath": "/pages/sys/home/index",
		"text": "工作台",
		"iconPath": "/static/aidex/tabbar/home_1.png",
		"selectedIconPath": "/static/aidex/tabbar/home_2.png"
	},
	{
		"pagePath": "/pages/sys/msg/index",
		"text": "消息",
		"iconPath": "/static/aidex/tabbar/msg_1.png",
		"selectedIconPath": "/static/aidex/tabbar/msg_2.png"
	},
	{
		"pagePath": "/pages/sys/aftersales/staff-order",
		"text": "售后进度",
		"iconPath": "/static/aidex/tabbar/book_1.png",
		"selectedIconPath": "/static/aidex/tabbar/book_2.png"
	},
	{
		"pagePath": "/pages/sys/user/index",
		"text": "我的",
		"iconPath": "/static/aidex/tabbar/my_1.png",
		"selectedIconPath": "/static/aidex/tabbar/my_2.png",
	}
]

export {
	publicBar,
	selfBar
}

3.配置Vuex

位置示例如下:

在这里插入图片描述

具体代码如下:

// 引入Vue和Vuex
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

// 创建一个新的Vuex store实例
const store = new Vuex.Store({
  state: {
    // 存储动态tabbar的数据
    dynamicTabbar: []
  },
  getters: {},
  actions: {
    changeTabbar({ commit }, payload) {
      // 使用commit方法提交一个mutation,更新state中的dynamicTabbar
      commit('updateTabbar', payload)
    }
  },
  mutations: {
    updateTabbar(state, payload) {
      state.dynamicTabbar = payload
    }
  }
})
export default store


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

具体代码如下:

在这里插入图片描述

5.登录页内引入自定义tabbar,根据角色进行登录验证

在这里插入图片描述

注:根据实际登录业务逻辑修改

在这里插入图片描述

6.在每个导航页中使用自定义的tabbar

项目中引入uView-UI的tabbar组件

在这里插入图片描述

具体使用如下:

    <!-- home/index.vue 页面 -->
    <u-tabbar v-model="current" :list="tabBarList" :active-color="activeColor" :inactive-color="inactiveColor"
      :border-top="borderTop" />
 
  data() {
      return {
         title: '首页', // 导航栏的标题,这会显示在页面的顶部或作为当前视图的标题
		 tabBarList: this.$store.state.dynamicTabbar, // 导航栏的列表项,来源于Vuex状态管理中的dynamicTabbar
         current: 0, // 当前激活的导航项的索引,0表示第一个导航项
         borderTop: true, // 控制是否有顶部边框,true表示有边框
         inactiveColor: '#909399', // 未激活的TabBarItem的颜色
         activeColor: '#5098FF' // 激活的TabBarItem的颜色
      }
  }
UniApp中,我们可以使用动态设置的方式来修改Tabbar的样式和内容。 一、修改Tabbar样式: 1. 在App.vue文件中,添加一个全局的tabbar样式变量,例如: ```javascript export default { globalData: { tabBarStyle: { backgroundColor: '#ffffff', color: '#000000', selectedColor: '#ff0000', borderStyle: 'white', list: [ { pagePath: 'pages/index/index', text: '首页', iconPath: 'static/tabBar/home.png', selectedIconPath: 'static/tabBar/home_selected.png' }, { pagePath: 'pages/category/category', text: '分类', iconPath: 'static/tabBar/category.png', selectedIconPath: 'static/tabBar/category_selected.png' }, // 其他tabbar项... ] } } } ``` 2. 在页面的onLoad钩子函数中,动态修改tabbar样式: ```javascript onLoad() { this.globalData.tabBarStyle.backgroundColor = '#f5f5f5'; this.globalData.tabBarStyle.color = '#666666'; this.globalData.tabBarStyle.selectedColor = '#ff9900'; } ``` 3. 修改完样式后,通过this.globalData.tabBarStyle.list属性可以获取到tabbar的列表数据,根据实际需要修改每个tabbar项的页面路径、文字、图标等。 二、修改Tabbar内容: 1. 在页面的onLoad钩子函数中,动态修改tabbar的内容: ```javascript onLoad() { this.globalData.tabBarStyle.list[0].text = 'New首页'; this.globalData.tabBarStyle.list[0].iconPath = 'static/tabBar/home_new.png'; this.globalData.tabBarStyle.list[0].selectedIconPath = 'static/tabBar/home_selected_new.png'; } ``` 2. 修改完内容后,可以通过this.globalData.tabBarStyle.list属性获取到每个tabbar项的具体内容,并进行修改。 通过以上方式,我们可以实现UniApp动态设置Tabbar的样式和内容。如果需要实时更新Tabbar的样式和内容,可以通过监听数据变化,动态更新页面显示。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值