小程序自定义tabbar,不同用户显示不同页面

前言

当我们拿到一个小程序项目,官方tabbar足够我们使用,但跟着业务有所增加,我们前端也不得不跟着改变。因为业务需求,我这个小程序分成了用户端和管理端两端,相对应使用权限也不同,因此tabbar所跳转页面也不同

管理端

 

 用户端

 做了很多功课,最终选择了自定义tabbar,下面介绍我的做法

1,新建一个tabbar.vue

tabbar.vue页面

 

<template>
    <block>
        <view v-for="(item,index) in tabList" :key="index" class="navigator"
            :class="currentTabIndex == index ? 'on' : ''" @click="switchTab1(index)">
            <view class="icon">
                <text class="iconfont" :class="item.icon"
                    :style="[currentTabIndex == index ? {'color': tintColor} : {'color': color}]"></text>
                <text v-if="item.badgeDot" class="uni_badge uni_badge_dot"></text>
            </view>
            <view class="text" :style="[currentTabIndex == index ? {'color': tintColor} : {'color': color}]">
                {{item.text}}</view>
        </view>
    </block>

</template>

script

<script>
	export default {
		data() {
			return {
               //需要跳转的路径和text
				tabList: [{
						page: '../../pages/index/index',
						text: '看板',
					},
					{
						page: '../../pages/index/manage/manage',
						text: '管理',
					},
					{
						page: '../../pages/index/my/my',
						text: '我的',
					}

				],
				currentTabIndex: this.current
			}
		},
		props: {
			current: {
				type: [Number, String],
				default: 0
			},
			backgroundColor: {
				type: String,
				default: '#fbfbfb'
			},
           //默认tabbar文字颜色
			color: {
				type: String,
				default: '#999'
			},
           //点击tabbar文字颜色
			tintColor: {
				type: String,
				default: '#42b983'
			}
		},
		methods: {
          //点击事件 ,点击text跳转相对应的路径
			switchTab1(index) {
				this.currentTabIndex = index
				this.$emit('click', index)
				uni.redirectTo({
					url: this.tabList[index].page
				})

			}
		},
	}
</script>

2.在main.js中添加

 

 3,在页面中使用

3.1   在index页面引入我们写好的tabbar组件

 3.2   在index页面中添加

<tab-bar :current="currentTabIndex" backgroundColor="#fbfbfb" color="#999" tintColor="#1B82D2"
			@click="tabClick"></tab-bar>

 

 3.3,在methods添加事件

	tabClick(index) {
				// console.log('返回tabBar索引:' + index)
				this.currentTabIndex = index
			},

 3.4 这样一个自定义tabbar就完成了,最后可以根据所需添加样式

 

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中的TabBar是用于展示小程序的主要入口和导航栏,用户可以通过它在不同功能模块之间进行切换。TabBar通常位于小程序底部,包含固定数量的选项卡,每个选项卡对应小程序的一个页面或功能。 以下是TabBar的基本使用和切换方法: 1. **配置TabBar**: 在`app.json`文件中,需要配置TabBar的项,包括标签名称、路径(对应的小程序页面路径)和图标等。每个TabBar项可以关联一个或多个页面。 ```json { "tabBar": { "color": "#666666", "selectedColor": "#007aff", "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "path/to/icon.png" }, // 添加其他TabBar项... ] } } ``` 2. **页面跳转**: 在对应的页面内部,可以使用`wx.navigateTo`、`wx.reLaunch`、`wx.switchTab`等API来实现页面间的切换。`wx.switchTab`用于在已存在的TabBar项中切换页面。 ```javascript wx.switchTab({ url: 'pages/second/second' // 要切换到的页面路径 }) ``` 3. **动态显示与隐藏**: 可以根据业务逻辑动态设置TabBar显示状态,比如某些功能在特定条件下不显示TabBar上。 4. **监听事件**: TabBar支持自定义事件,可以通过`onTabItemTap`事件监听用户点击某个TabBar项。 5. **设置默认页**: 在`options.json`中可以设置默认加载的第一个页面,也可以在启动时通过`Page({})`配置。 ```javascript Page({ options: { tabBar: { selectedColor: '#007aff' } } }) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值