uniapp自定义Tabbar教程

uniapp自定义Tabbar

1、定义tabbar

在pages.json中配置除主要页面地址,

json 复制代码
	"tabBar": {
		"custom": true,
		"list": [{
				"pagePath": "pages/home/index"
			},
			{
				"pagePath": "pages/user-center/index"
			}
		]
	},

2、创建自定义Tabbar组件

vue 复制代码
<template>
	<up-tabbar :value="selected" @change="handleChange" :fixed="true" :placeholder="true" :safeAreaInsetBottom="true">
		<up-tabbar-item v-for="item in tabbarList" :key="item.text" :text="item.text" :icon="item.icon" />
	</up-tabbar>
</template>

<script setup lang="ts">
	import { reactive, ref } from 'vue'
	const props = defineProps({
		selected: {
			// 当前选中的tab index
			type: Number,
			default: 1,
		},
	});

	const tabbarList = reactive([
		{
			text: "首页",
			icon: "home",
			pagePath: "/pages/home/index"
		},
		{
			text: "我的",
			icon: "account",
			pagePath: "/pages/user-center/index"

		}
	])

	function handleChange(index) {
		console.log('tab ' + index)
		const tarbar = tabbarList[index]
		// 跳转到其他页面
		uni.switchTab({
			url: tarbar.pagePath
		})
	}
</script>

<style scoped lang="scss">
</style>

3、在以上定义的主页面中加入以下的代码,每个页面都要加

vue 复制代码
<template>
	// ........
	
	// 这里的selected很重要,标识这里是第一个页面,如果是第二个 这里就是2
	<MyTabbarVue :selected="1" />
</template>

<script setup lang="ts">
	import { onShow } from '@dcloudio/uni-app';
	import MyTabbarVue from '../../components/MyTabbar.vue';

	// 这里主要是为了无感隐藏原来的tabbar
	onShow(() => {
		uni.hideTabBar({
			animation: false
		})
	})
</script>
相关推荐
独泪了无痕28 分钟前
Vue调试神器:Vue DevTools使用指南
vue.js·前端工程化
优秀稳妥的JiaJi5 小时前
基于腾讯地图实现电子围栏绘制与校验
前端·vue.js·前端框架
Lee川5 小时前
深度解构JavaScript:作用域链与闭包的内存全景图
javascript·面试
好雨知时节t5 小时前
Pinia中defineStore的使用方法
vue.js
_Eleven6 小时前
Pinia vs Vuex 深度解析与完整实战指南
前端·javascript·vue.js
技术狂小子6 小时前
# 一个 Binder 通信中的多线程同步问题
javascript·vue.js
进击的尘埃7 小时前
Service Worker + stale-while-revalidate:让页面"假装"秒开的那些事
javascript
秋水无痕7 小时前
从零搭建个人博客系统:Spring Boot 多模块实践详解
前端·javascript·后端
进击的尘埃7 小时前
基于 Claude Streaming API 的多轮对话组件设计:状态机与流式渲染那些事
javascript
juejin_cn8 小时前
[转][译] 从零开始构建 OpenClaw — 第六部分(持久化记忆)
javascript