uniapp使用自定义tabbar时实现跳转tabbar不刷新

uniapp使用自定义tabbar时实现跳转tabbar不刷新

  • 一、问题:
  • 二、解决步骤

一、问题:

使用uni-app开发时,又是并不想使用原生tabbar!但是使用自定义tabbar在界面之间跳转时总会出现整个界面包括tabbar也刷新的情况!

二、解决步骤

  1. 首先要配置原生tabbar,但是要隐藏!

先在,pages.json中配置tabbar,如下,设置高度为0,界面只需要设置路径

"tabBar":{
		"list": [
			{
				"pagePath":"pages/index/index"
			},{
				"pagePath":"pages/tuan/tuan"
			},{
				"pagePath":"pages/order/order"
			},{
				"pagePath":"pages/my/my"
			}
		],
		"height":"0px"
	}
  1. 在app.vue中隐藏原生tabbar
onLaunch: function() {
			// 隐藏原生tarbar (这里因为用自定义tarbar跳转时闪白屏,所以这里用一种特殊的方式)
			uni.hideTabBar()
			console.log('App Launch')
		},
  1. 自己定义一个tabbar的component,并在全局挂载

自定义tarbar的代码如下,图标和路径自行改变

<template>
	<view v-if="showselected">
		<view class="tabbar">
			<view class="box">
				<view class="item" v-for="(item,index) in tabBar.list" :key="item.pagePath"
					@click="switchTab(index,item)">
						
						<view class="icon"
							v-if="item.flag == 'icon'">
							<image :src="item.iconPath" class="icon" v-if="selectedIndex !== index">
							<image :src="item.selectedIconPath" class="icon" v-else>
							<view>
								<text :class="['item-text',{'text-active':selectedIndex === index}]">{{item.text}}</text>
							</view>
						</view>
						
						<view class="icon seven"
							v-else>
							<image :src="item.iconPath" mode=""></image>
						</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				selectedIndex: uni.getStorageSync('selectedIndex') || 0,  // 是否选中
				showselected: true, // 是否启用tarBer
				tabBar: {
					list: [{
							"flag": 'icon',
							"pagePath": "pages/index/index",
							"iconPath": "/static/tabber/index.png",
							"selectedIconPath": "/static/tabber/indexAction.png",
							"text": "首页"
						},{
							"flag": 'icon',
							"pagePath": "pages/tuan/tuan",
							"iconPath": "/static/tabber/tuan.png",
							"selectedIconPath": "/static/tabber/tuanAction.png",
							"text": "团购"
						},{
							"flag": 'seven',
							"pagePath": "/",
							"iconPath": "/static/tabber/seven.png",
							"selectedIconPath": "/",
							"text": ""
						},{
							"flag": 'icon',
							"pagePath": "pages/order/order",
							"iconPath": "/static/tabber/order.png",
							"selectedIconPath": "/static/tabber/orderAction.png",
							"text": "订单"
						},{
							"flag": 'icon',
							"pagePath": "pages/my/my",
							"iconPath": "/static/tabber/my.png",
							"selectedIconPath": "/static/tabber/myAction.png",
							"text": "我的"
						}
					]
				},
			}
		},
		methods: {
			switchTab(index, item) {
				let url = '/' + item.pagePath
				let pagePath = url
				uni.switchTab({url})
				this.tabBar.list.forEach((v, i) => {
					if (item.pagePath === v.pagePath) {
						uni.setStorageSync('selectedIndex', index);
					}
				})
			}
		},
		
	}
</script>

<style lang="scss">
	
	.tabbar {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 130rpx;
		z-index: 999;
		background: #ffffff;
		border-top: 2rpx solid #eee;
		
		.box {
			padding: 0 40rpx;
			display: flex;
			justify-content: space-around;
			.item {
				width: 15%;
				
				.icon {
					margin-top: 10rpx;
					width: 100%;
					font-size: 24rpx;
					color: #8a8a8a;
					
					text-align: center;
					image {
						width: 50rpx;
						height: 50rpx;
					}
					
					.text-active {
						color: #c5cde6;
					}
				}
				.seven {
					height: 100rpx;
					background-color: #dcdaef;
					border-radius: 0rpx 0rpx 35rpx 35rpx;
					line-height: 130rpx;
					
					image {
						width: 70rpx;
						height: 70rpx;
					}
				}
			}
		}
	}
	 
</style>

在main.js 全局挂载

// 挂在全局自定义的tarbar
import tabBar from '@/components/tabbar/tabBar.vue'
Vue.component('tabbar',tabBar) //挂载
  1. 在引用界面采用
    uniapp使用自定义tabbar时实现跳转tabbar不刷新_第1张图片
    三、 其他注意事项

这样的话四个主界面可以基于路由占位的方式进行组件化开发!

文章参考: 参考1参考2

你可能感兴趣的:(前端技术小栈,uni-app,javascript,前端)