微信小程序怎么实现非tabbar页面显示tabbar,自定义组件实现

微信小程序没有发现可以实现非tabbar页面显示tabbar的方法,但是可以在tabbar页面当中隐藏tabbar,使用wx.hideTabBar()方法就可以实现,在非tabbar页面调用wx.showTabBar()方法却会显示失败,不能显示tabbar

javascript 复制代码
        onLoad() {
			wx.showTabBar({
				success() {
					console.log('success')
				},
				fail(err) {
					console.log(err, 'fail')
				}
			})
		},    

报错如下:显示该页面非tabbar页面


可以选择自定义组件组件实现,代码实现如下:

在项目根目录下面创建一个组件文件(/component/tabbar/index.vue)

javascript 复制代码
<template>
	<view>
		<view class="tabbar">
			<u-tabbar :value="value1" @change="tabbarChange" :fixed="false" :placeholder="false"
				:safeAreaInsetBottom="false">
				<u-tabbar-item text="求职者" icon="home"></u-tabbar-item>
				<u-tabbar-item text="聊天" icon="photo"></u-tabbar-item>
				<u-tabbar-item text="通讯录" icon="play-right"></u-tabbar-item>
				<u-tabbar-item text="我的" icon="account"></u-tabbar-item>
			</u-tabbar>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value1: 0,
				url: '/pages/A/index'
			};
		},
		onLoad() {
			this.tabbarChange()
		},
		methods: {
			tabbarChange(index) {
				console.log('click', index);
				this.value1 = index
				tabbarIndex === 0 ? this.url = '/pages/A/index' : ''
				tabbarIndex === 1 ? this.url = '/pages/B/index' : ''
				tabbarIndex === 2 ? this.url = '/pages/C/index' : ''
				tabbarIndex === 3 ? this.url = '/pages/D/index' : ''
				wx.navigateTo({
					url: this.url,
				})
			}
		}
	}
</script>

<style lang="less">
	.tabbar {
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 99;
		box-shadow: 0rpx -8rpx 16rpx 0rpx rgba(208, 208, 208, 0.3);
	}
</style>

创建好了之后需要微信小程序里面的page.json文件引入

javascript 复制代码
{
    "usingComponents": {
		"tabbar": "../../components/tabbar/index"
	},
}

然后在需要tabbar页面当中进行注册并使用,这样就可以实现在任何需要tabbar的页面当中展示tabbar了

javascript 复制代码
<template>
    <view>
        <tabbar></tabbar>
    </view>
</template>

<script>
	import tabbar from '../../../component/tabbar/index.vue'
	export default {
		components: {
			tabbar
		},
		data() {
			return {
				
			}
		},
	}
</script>

不过在这个实现过程中使用了uview组件库,目前来看这个组件库还是能够满足小程序开发中的需求的,有常见的一些功能,很方便使用

介绍 | uView 2.0 - 全面兼容 nvue 的 uni-app 生态框架 - uni-app UI 框架

安装和配置在官网中都有相关使用步骤,可以查看!

相关推荐
遇到困难睡大觉哈哈6 小时前
Harmony os 静态卡片(ArkTS + FormLink)详细介绍
前端·microsoft·harmonyos·鸿蒙
用户47949283569157 小时前
Bun 卖身 Anthropic!尤雨溪神吐槽:OpenAI 你需要工具链吗?
前端·openai·bun
p***43487 小时前
前端在移动端中的网络请求优化
前端
g***B7387 小时前
前端在移动端中的Ionic
前端
游戏开发爱好者87 小时前
iOS 开发者的安全加固工具,从源码到成品 IPA 的多层防护体系实践
android·安全·ios·小程序·uni-app·cocoa·iphone
拿破轮7 小时前
使用通义灵码解决复杂正则表达式替换字符串的问题.
java·服务器·前端
whltaoin7 小时前
【 Web认证 】Cookie、Session 与 JWT Token:Web 认证机制的原理、实现与对比
前端·web·jwt·cookie·session·认证机制
Aerelin8 小时前
爬虫playwright入门讲解
前端·javascript·html·playwright
玄微云8 小时前
如何选择可靠的产后修复营销小程序?市场分析与实用指南
小程序
5***o5008 小时前
前端在移动端中的NativeBase
前端