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>
相关推荐
计算机程序设计开发7 分钟前
基于Node.js+Express+MySQL+VUE实现的计算机毕业设计旅游推荐网站
vue.js·spring boot·课程设计·旅游·计算机毕设·计算机毕业设计
GISer_Jing22 分钟前
React跨平台
前端·javascript
qq_5443291725 分钟前
从0学习React(3)
开发语言·javascript·ecmascript
@山海@28 分钟前
React 中的无限滚动加载数据实现
前端·javascript·react.js
南乔几经秋_35 分钟前
安装 Nuxt.js
开发语言·javascript
掘金安东尼1 小时前
上周前端发生哪些新鲜事儿? #382
前端·javascript·面试
_斯洛伐克2 小时前
关于vue2+uniapp+uview+vuex 私募基金项目小程序总结
前端·小程序·uni-app
zpjing~.~2 小时前
uniapp中h5环境添加console.log输出
前端·javascript·uni-app
骨子里的偏爱2 小时前
uniapp在线打包的ios后调用摄像头失败的解决方法
缓存·uni-app
计算机学姐2 小时前
基于微信小程序的旧衣回收系统
java·vue.js·spring boot·微信小程序·小程序·tomcat·mybatis