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>
相关推荐
1***815326 分钟前
React组件
前端·javascript·react.js
6***34940 分钟前
Vue混合现实案例
前端·vue.js·mr
p***43481 小时前
Vue混合现实开发
前端·vue.js·mr
__花花世界1 小时前
前端日常工作开发技巧汇总
前端·javascript·vue.js
www_stdio1 小时前
栈(Stack)详解:从原理到实现,再到括号匹配应用
javascript
iOS阿玮2 小时前
打个广告,帮忙招一个iOS开发的扛把子~
uni-app·app·apple
JIngJaneIL2 小时前
智慧物业|物业管理|基于SprinBoot+vue的智慧物业管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·论文·智慧物业管理系统
爬坑的小白2 小时前
vue 2.0 路由跳转时新开tab
前端·javascript·vue.js
爬坑的小白2 小时前
vue x 状态管理
前端·javascript·vue.js
凌览2 小时前
一键去水印|5 款免费小红书解析工具推荐
前端·javascript·后端