uniapp实现自定义tabbar【直用版】

1.pages.json中将tabBar下面全部注释掉

2.在common文件夹下面建一个tabbar文件夹,里面放个tabbar.vue(位置,名称自己随意)

tabbar.vue:(直接复制)

<template>
	<view class="tabbar">
		<view class="tab" v-for="(item,index) in tabbarList" :key="index" @tap='navigatorTo(item.pagePath)'>			
				<image v-if="item.pagePath === 'release'" :src="item.iconPath" class="addIco" @click="to_release"></image>
				<view  v-else>
					<image v-if="item.pagePath === cureentPage" :src="item.selectedIconPath"></image>
					<image v-else :src="item.iconPath"></image>
				</view>
				<!-- <view class="text"> {{item.text}} </view>		 -->
		</view>
	</view>
</template>
<script>
	export default {
		props:{
			cureentPage:{
				type:String,
				default:'index'
			}
		},		
	}
</script>
<script setup>
	const to_release=()=>{ //跳转发布页面
		uni.navigateTo({
			url:"/pages/release/release"
		})
	}
	const tabbarList=[  //自己定义的tabber
		{
			"pagePath": "index",
			"iconPath": "http://hwww.fun:6060/tabbar/zhu_pi.png",
			"selectedIconPath": "http://hwww.fun:6060/tabbar/zhu1_pi.png",
			"text": "主页"
		},
		{
			"pagePath": "release",
			"iconPath": "http://hwww.fun:6060/tabbar/add.png",
			// "selectedIconPath": "/static/img/tabbar/new2/sort1_pi.png",
			// "text": "发布"
		},
		{
			"pagePath": "myinfo",
			"iconPath": "http://hwww.fun:6060/tabbar/user_pi.png",
			"selectedIconPath": "http://hwww.fun:6060/tabbar/user1_pi.png",
			"text": "你的"
		}
	]
	
	const navigatorTo=(e)=>{  //当点击下方tabbar跳转指定页面
		console.log(e);
			uni.redirectTo({
				url:`../../pages/${e}/${e}`
			})
	}
</script>

<style scoped>
   .tabbar{
	   /* border-top:1rpx solid #c5c4c5; */
	   background-color:#FFFF ;
	   z-index: 999;
	   position: fixed;
	   left: 0;
	   bottom: 0;
	   width: 100%;
	   height: 120rpx;
	   display: flex;
	   justify-content: space-around;
	   align-items: center;
   }
   .tab{
	   display: flex;
	   flex-direction: column;
	   justify-content: center;
	   align-items: center;
   }
   image{
	   width: 50rpx;
	   height: 50rpx;
   }
   .addIco{
	   width: 50px;
	   height: 50px;
   }
</style>

3.在需要用到tabbar的位置(index,myinfo)中引入

index.vue:

<script setup>
    import Tabbar from '@/common/tabbar/tabbar.vue'
</script>

 下面代码直接放当前页面底部就行,注意传递了一个’index‘的值,这个值与tabbar中的pagePath是对应的,以此来判断该跳转哪个页面

<template>
	<Tabbar cureentPage='index'></Tabbar>
</template>

myinfo.vue

<script setup>
    import Tabbar from '@/common/tabbar/tabbar.vue'
</script>
<template>
 <Tabbar cureentPage='myinfo'></Tabbar>
</template>

 现在可以用了

### 创建自定义底部 TabBar 在 `components` 目录下建立一个新的组件文件名为 `CustomTabBar.vue` 来开发自定义组件[^1]。 ```html <template> <view class="custom-tab-bar"> <!-- 定义底部导航栏的内容 --> <view v-for="(item, index) in tabBarList" :key="index" @click="switchPage(item.pagePath)"> <image :src="currentIndex === index ? item.selectedIconPath : item.iconPath"></image> <text :class="{ active: currentIndex === index }">{{ item.text }}</text> </view> </view> </template> <script setup> import { ref } from 'vue' const props = defineProps({ current: { type: Number, default: 0 } }) // 底部标签栏列表数据 const tabBarList = [ { pagePath: '/pages/index/index', iconPath: '/static/images/home.png', selectedIconPath: '/static/images/home-active.png', text: '首页' }, { pagePath: '/pages/category/index', iconPath: '/static/images/category.png', selectedIconPath: '/static/images/category-active.png', text: '分类' }, { pagePath: '/pages/my/index', iconPath: '/static/images/my.png', selectedIconPath: '/static/images/my-active.png', text: '我的' } ] let currentIndex = ref(props.current) function switchPage(pagePath) { uni.switchTab({ url: pagePath }) } </script> <style scoped> .custom-tab-bar { display: flex; justify-content: space-around; align-items: center; height: 50px; } .active { color: blue; } </style> ``` 此代码展示了如何构建一个简单的自定义底部 TabBar 组件,其中包含了三个选项卡:“首页”,“分类” 和 “我的”。每个项都有未选中状态下的图标路径 (`iconPath`) 和选中状态下图标路径 (`selectedIconPath`)。当点击某个项目时会触发 `switchPage()` 方法来切换页面[^2]。 为了使这个自定义组件生效,在需要显示的地方引入并注册该组件: ```html <!-- BaseApp/pages/my.vue 文件中的模板部分 --> <template> <view> <text class="title">{{ title }}</text> <custom-tab-bar :current="2"></custom-tab-bar> <!-- 使用自定义组件 --> </view> </template> <script setup> import CustomTabBar from '../../components/CustomTabBar.vue' import { ref } from 'vue' let title = ref('我的') </script> ``` 这里通过 `<custom-tab-bar>` 标签使用了之前创建好的自定义组件,并传递了一个属性 `current` 表示当前激活的是第三个选项卡(索引从零开始计数),即 "我的"。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值