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>
相关推荐
老胡说前端6 分钟前
JS 数组相同的key 进行合并
前端·javascript·vue.js
S01d13r9 分钟前
LeetCode 解题思路 33(Hot 100)
javascript·算法·leetcode
kovlistudio14 分钟前
红宝书第三十二讲:零基础学会模块打包器:Webpack、Parcel、Rollup
前端·javascript·webpack·node.js
天天进步201516 分钟前
Java全栈项目--校园快递管理与配送系统(5)
java·javascript·vue.js
念九_ysl43 分钟前
使用 Vue 快速集成 FullCalendar 日历组件教程
前端·javascript·vue.js
造夢先森1 小时前
SpringAI+MCP协议 实战
前端·javascript·easyui
键指江湖1 小时前
React 列表渲染
javascript·react.js·ecmascript
Jyywww1211 小时前
Vue2+Vue3 45-90集学习笔记
vue.js·学习
bst@微胖子1 小时前
Flutter之页面布局二
android·javascript·flutter
好_快2 小时前
Lodash源码阅读-findIndex
前端·javascript·源码阅读