uniapp 自定义tabBar导航栏

1.在App.vue文件里把原生的tabBar导航栏,隐藏掉

//App.vue
onLaunch: function() {
        //隐藏tabBar   
		uni.hideTabBar()
		uni.removeStorageSync('selectedIndex');
	},

2.在components文件下,创建一个uni-tab-bar.vue文件

<template>
	<!-- <view class="tabBars">
		<view class="tabs" v-for="(item,index) in tabList" :key="index" @tap="switchTab(index)">
			<image class="imgs" :src="$jointImage(item.selectedIconPath)" v-if="item.state"></image>
			<image class="imgs" :src="$jointImage(item.iconPath)" v-else></image>
			<view class="text" :style="{color:isActive(item.pagePath) ? ' #76bd21' : '' }">{{item.text}}</view>
		</view>
	</view> -->
	<uni-transition mode-class="fade" :duration="200" :show="true">
		<view>
			<view class="tab-content">
				<slot />
			</view>
			<view class="tabbar">
				<view class="navigator">
					<view ref='warpper' class="warpper">
						<view ref="navItem" class="navigator-item" v-for="(item,index) in tabList" :key="item.pagePath"
							@click="switchTab(item,index)" :data-index='index'>
							<image class="icon" :src="$jointImage(item.iconPath)" v-if="selectedIndex !== index">
							</image>
							<image class="icon" :src="$jointImage(item.selectedIconPath)" v-else></image>
							<text :class="['item-text',{'text-active':selectedIndex === index}]">{{item.text}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</uni-transition>
</template>

<script>
	export default {
		data() {
			return {
				selectedIndex: uni.getStorageSync('selectedIndex') || 0,
				configuration: {},
				tabList: [],
			};
		},
		mounted() {
			this.alliconSet()
		},
		methods: {
			alliconSet() {
				uni.$http.post('index/iconSet', {}).then(res => {
					this.configuration = res.data.data
					this.tabList = [{
							pagePath: "/pages/index/index",
							text: "首页",
							state: true,
							iconPath: this.configuration.tabbar1_nocheck,
							selectedIconPath: this.configuration.tabbar1_check
						},

						{
							pagePath: "/pages/menu/menu",
							text: "点单",
							state: false,
							iconPath: this.configuration.tabbar2_nocheck,
							selectedIconPath: this.configuration.tabbar2_check
						},
						{
							pagePath: "/pages/order_two/order_two",
							text: "订单",
							state: false,
							iconPath: this.configuration.tabbar3_nocheck,
							selectedIconPath: this.configuration.tabbar3_check
						},
						{
							pagePath: "/pages/my/my",
							text: "我的",
							state: false,
							iconPath: this.configuration.tabbar4_nocheck,
							selectedIconPath: this.configuration.tabbar4_check
						},
					]
				})
			},
			switchTab(items, indexs) {
				uni.switchTab({
					url: `${this.tabList[indexs].pagePath}`
				});
				this.tabList.forEach((v, i) => {
					if (items.pagePath === v.pagePath) {
						uni.setStorageSync('selectedIndex', indexs);
					}
				})

			},
		},
	};
</script>

<style lang="scss" scoped>
	.tabbar {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 140rpx;
		z-index: 999;
		background: #F5F5F5;
		border-top: 2rpx solid #eee;
	}

	.navigator {
		width: 85%;
		margin: 0 auto;
		padding: 20rpx;
		overflow: hidden;
	}

	.warpper {
		display: flex;
		justify-content: space-between;
		width: auto;
		transition-timing-function: ease-out;
	}

	.navigator-item {
		display: flex;
		align-items: center;
		flex-direction: column;
		width: 50rpx;
		height: 100%;
	}

	.item-text {
		margin-top: 6rpx;
		color: #777E86;
		font-size: 24rpx;
	}

	.text-active {
		color: #76bd21 !important;
	}

	.icon {
		width: 24px;
		height: 24px;
	}
</style>

3.在main.js引入组件

import UniTabBar from '@/components/uni-tab-bar.vue'
Vue.component('UniTabBar', UniTabBar)

4.在需要的页面,使用组件

//index.vue

<template>
	<view>
		<UniTabBar />
	</view>
</template>

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
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 ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泥木马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值