uniapp自定义tabBar,根据角色展示路由导航

14 篇文章 0 订阅
tabBar上常用属性:
backgroundColor:tabBar 的背景色
borderStyle:tabBar 上边框的颜色
selectedIconPath:选中时的图片路径
iconPath:未选中时的图片路径
selectedColor:tab 上的文字选中时的颜色
color:tab 上文字的默认颜色

tabBar 中的 list 是一个数组,只能配置最少2个、最多5tabtab 按数组的顺序排序。
当前要根据角色展示tabBar,角色不同展示的tabBar不同。

pages.json页面添加所有要展示的tabBar

注意:!!!custom:true 属性是true时,确定要使用自定义tabBar

	"tabBar":{
		   "custom": true, // 是否使用自定义tabBar
	       "color":"#666",
	       "selectedColor":"#f3980f",
	       "backgroundColor":"#fff",
		   // #ifdef APP-PLUS
	       "borderStyle":"white",
		   // #endif
			"height":"70px",
	       "list":[
	           {
	               "pagePath":"pages/index/index",
	               "text":"跟踪单",
	               "iconPath":"./static/image/gzd01.png",
	               "selectedIconPath":"static/image/gzd.png"
	           },
	           {
	               "pagePath":"pages/bchy/index",
	               "text":"包车货源",
	               "iconPath":"static/image/bchy01.png",
	               "selectedIconPath":"static/image/bchy_act.png"
	           },
	           {
	               "pagePath":"pages/zchy/index",
	               "text":"整车货源",
	               "iconPath":"static/image/zchy.png",
	               "selectedIconPath":"static/image/zchy01.png"
	           },
			   {
				 "pagePath":"pages/ldhy/index",
				 "text":"零担货源",
				 "iconPath":"static/image/ldhy.png",
				 "selectedIconPath":"static/image/ldhy01.png"
			   },
			   {
				"pagePath":"pages/wode/index",
				"text":"我的",
				"iconPath":"static/image/wode.png",
				"selectedIconPath":"static/image/wode01.png"
			   }
	       ]
	   }
在和pages目录同级创建 custom-tab-bar 的文件夹
custom-tab-bar/index.js
custom-tab-bar/index.json
custom-tab-bar/index.wxml
custom-tab-bar/index.wxss
index.wxml添加tabBar的结构代码
<!--miniprogram/custom-tab-bar/index.wxml-->
<cover-view class="tab-bar">
    <cover-view class="tab-bar-border"></cover-view>
    <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
        <cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
        <cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
    </cover-view>
</cover-view>
index.wxss 添加样式
.tab-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 48px;
    background: white;
    display: flex;
    padding-bottom: env(safe-area-inset-bottom);
}

.tab-bar-border {
    background-color: rgba(0, 0, 0, 0.33);
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 1px;
    transform: scaleY(0.5);
}

.tab-bar-item {
    flex: 1;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.tab-bar-item cover-image {
    width: 27px;
    height: 27px;
}

.tab-bar-item cover-view {
    font-size: 10px;
}

index.json 文件添加
{
  "component": true,
  "usingComponents": {}
}

index.js 文件添加数据和事件方法,角色判断
// custom-tab-bar/index.js.js

Component({

    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        selected: null,
		color: "#666",
		selectedColor: "#f3980f",
		backgroundColor: "#fff",
		borderStyle: "white",
		height: "70px",
        list: [],
        list1: [
            {
                pagePath: "/pages/index/index",
                text: "跟踪单",
                iconPath: "/static/image/gzd01.png",
                selectedIconPath: "/static/image/gzd.png",
            },
            {
                pagePath: "/pages/bchy/index",
                text: "包车货源",
                iconPath: "/static/image/bchy01.png",
                selectedIconPath: "/static/image/bchy_act.png",
            },
            {
                pagePath: "/pages/zchy/index",
                text: "整车货源",
                iconPath: "/static/image/zchy.png",
                selectedIconPath: "/static/image/zchy01.png",
            },
            {
                pagePath:"/pages/ldhy/index",
                text:"零担货源",
                iconPath:"/static/image/ldhy.png",
                selectedIconPath:"/static/image/ldhy01.png"
            },
            {
                pagePath: "/pages/wode/index",
                text: "我的",
                iconPath: "/static/image/wode.png",
                selectedIconPath: "/static/image/wode01.png",
            }],
        list2: [
			{
				pagePath: "/pages/index/index",
				text: "跟踪单",
				iconPath: "/static/image/gzd01.png",
				selectedIconPath: "/static/image/gzd.png"
			},
			{
				pagePath: "/pages/wode/index",
				text: "我的",
				iconPath: "/static/image/wode.png",
				selectedIconPath: "/static/image/wode01.png"
			}],
    },

    attached() {
        // utype 角色10 时控制展示tabBar
        // !!! 切换用户后没有重新走一遍 custom-bar-ber 角色不同下面展示菜单会有缓存 可以在每个页面写一边设置list
        const roleId = wx.getStorageSync('userInfo').utype;
        if (roleId == 10) {
            this.setData({
            	list: this.data.list2
            })
            console.log('这里是自定义总组件 角色10');
        }else {
            this.setData({
                list: this.data.list1
            })
            console.log('这里是自定义总组件 角色其他');
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        switchTab(e) {
            const data = e.currentTarget.dataset
            const url = data.path
            // 处理多次点击
            if (data.index === this.data.selected) {
                console.log('多次点击');
                return false;
            }
            wx.switchTab({url})
			// this.setData({
			// 	selected: data.index
			// })

        }
    }
})

!!!tabBar图标切换 要点击两次才能有选中状态,继续往下走

在每个tabBar目录(index)页面的onShow生命周期中配置
pages/index/index

注意:selected 是当前页面路由展示索引,需和tabBarlist顺序匹配;
注意:shippingUsergeneralUser 是在utils/tabBar.js 中枚举的目录导航,内容如下(在最下面)import {generalUser, shippingUser} from "../../utils/tabBar"

		onShow(){
	      // 根据角色自定义tabBar
	      const roleId = uni.getStorageSync('userInfo').utype;
	      if (typeof this.$mp.page.getTabBar === 'function' &&
	          this.$mp.page.getTabBar()) {
	        if (roleId == 10) {
	          this.$mp.page.getTabBar().setData({
	            list: shippingUser,
	            selected: 0
	          })
	          console.log('角色10');
	        }else {
	          this.$mp.page.getTabBar().setData({
	            list: generalUser,
	            selected: 0
	          })
	          console.log('角色其他');
	        }
	      }


		},
pages/bchy/index
		onShow() {
		  // 根据角色展示tab菜单
	      const roleId = uni.getStorageSync('userInfo').utype;
	      if (typeof this.$mp.page.getTabBar === 'function' &&
	          this.$mp.page.getTabBar()) {
	        if (roleId == 10) {
	          this.$mp.page.getTabBar().setData({
	            list: shippingUser,
	          })
	        }else {
	          this.$mp.page.getTabBar().setData({
	            list: generalUser,
	            selected: 1
	          })
	        }
	      }

		},
pages/zchy/index
		onShow() {
	      // 自定义tabBar
	      const roleId = uni.getStorageSync('userInfo').utype;
	      if (typeof this.$mp.page.getTabBar === 'function' &&
	          this.$mp.page.getTabBar()) {
	        if (roleId == 10) {
	          this.$mp.page.getTabBar().setData({
	            list: shippingUser,
	          })
	        }else {
	          this.$mp.page.getTabBar().setData({
	            list: generalUser,
	            selected: 2
	          })
	        }
	      }

		},
pages/ldhy/index
		onShow() {
	      // 自定义tabBar
	      this.role = uni.getStorageSync('userInfo').utype;
	      if (typeof this.$mp.page.getTabBar === 'function' &&
	          this.$mp.page.getTabBar()) {
	        if (this.role == 10) {
	          this.$mp.page.getTabBar().setData({
	            list: shippingUser,
	          })
	        }else {
	          this.$mp.page.getTabBar().setData({
	            list: generalUser,
	            selected: 3
	          })
	        }
	      }
		},
pages/wode/index
		onShow() {
	      // 自定义tabBar
	      this.role = uni.getStorageSync('userInfo').utype;
	
	      if (typeof this.$mp.page.getTabBar === 'function' &&
	          this.$mp.page.getTabBar()) {
	        if (this.role == 10) {
	          this.$mp.page.getTabBar().setData({
	            list: shippingUser,
	            selected: 1
	          })
	        }else {
	          this.$mp.page.getTabBar().setData({
	            list: generalUser,
	            selected: 4
	          })
	        }
	      }
		},
utils/tabBar.js
// 10的用户
export const shippingUser =  [
    {
        pagePath: "/pages/index/index",
        text: "跟踪单",
        iconPath: "/static/image/gzd01.png",
        selectedIconPath: "/static/image/gzd.png",
    },
    {
        pagePath: "/pages/wode/index",
        text: "我的",
        iconPath: "/static/image/wode.png",
        selectedIconPath: "/static/image/wode01.png",
    }]

// 默认
export const generalUser = [
    {
        pagePath: "/pages/index/index",
        text: "跟踪单",
        iconPath: "/static/image/gzd01.png",
        selectedIconPath: "/static/image/gzd.png"
    },
    {
        pagePath: "/pages/bchy/index",
        text: "包车货源",
        iconPath: "/static/image/bchy01.png",
        selectedIconPath: "/static/image/bchy_act.png"
    },
    {
        pagePath: "/pages/zchy/index",
        text: "整车货源",
        iconPath: "/static/image/zchy.png",
        selectedIconPath: "/static/image/zchy01.png"
    },
    {
        pagePath:"/pages/ldhy/index",
        text:"零担货源",
        iconPath:"/static/image/ldhy.png",
        selectedIconPath:"/static/image/ldhy01.png"
    },
    {
        pagePath: "/pages/wode/index",
        text: "我的",
        iconPath: "/static/image/wode.png",
        selectedIconPath: "/static/image/wode01.png"
    }]

export default {
    shippingUser,
    generalUser
}

最终效果:

自定义tabbar

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
uniapp中实现自定义tabbar导航栏的方法有多种。根据引用内容,可以看到有两种方法可以实现。 第一种方法是在父组件中引入tabbar组件,并在onLoad生命周期函数中隐藏原有的tabbar。然后在模板中使用tabbar组件,并通过routePath属性指定每个tab对应的页面路径。通过这种方式,可以实现自定义的底部导航栏。\[1\] 第二种方法是创建一个总页面Index,并在模板中引入自定义tabbar组件。然后在总页面Index中引入自己写好的4个主页面(Home、Work、Message、My),并通过条件渲染的方式控制页面的切换。通过这种方式,可以实现自定义的底部导航栏。\[2\]\[3\] 以上是两种常见的实现自定义tabbar导航栏的方法,你可以根据自己的需求选择其中一种来实现。 #### 引用[.reference_title] - *1* [uniApp自定义tabBar导航](https://blog.csdn.net/qq_1307495/article/details/129584773)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [uni-app中如何自定义实现tabbar导航栏](https://blog.csdn.net/weixin_70811193/article/details/127946635)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值