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>
相关推荐
竹林8181 分钟前
RainbowKit快速集成多链钱包连接,我如何从“连不上”到“丝滑切换”
前端·javascript
No8g攻城狮18 分钟前
【前端】Vue 中 const、var、let 的区别
前端·javascript·vue.js
fishmemory7sec39 分钟前
Vue大屏自适应容器组件:v-scale-screen
前端·javascript·vue.js
饺子不吃醋40 分钟前
Promise原理、手写与 async、await
前端·javascript
CHB44 分钟前
uni-task - 轻量级团队任务管理系统
uni-app
前端那点事1 小时前
Vue3+TS 中 this 指向机制全解析(实战避坑版)
vue.js
糯米团子7492 小时前
react速通-3
javascript·react.js·前端框架
心连欣2 小时前
从静态页面到动态交互:DOM操作的核心API解析
前端·javascript·api
橙某人2 小时前
SSR页面上的按钮点不了?Nuxt 懒加载水合揭秘💧
前端·vue.js·nuxt.js
零瓶水Herwt2 小时前
Javascript常用设计模式
javascript