解决tabbar的动态配置,以及切换tabbar闪烁

业务需求:根据不同的权限进行不同的tabbar配置,同时解决切换tabbar,tabbar闪烁的问题,需要用到uview组件。

首先在pages里面按照目录结构创建页面
在这里插入图片描述创

在pages里面写入home的路径
在这里插入图片描述

home.vue template里面的写法

<template>
  <view>
	<view class="the-home">
			//所有tabbar页面
			//动态组件加载
			<component :is="component"></component>
	</view>
	// 动态tabbar的加载
	<u-tabbar class="tab-bar" v-model="current" :list="tabbar" @change="changeTb"></u-tabbar>
  </view>
</template>

在home页面里面引入需要的tabbar组件

	import Editor from './components/editor/editor.vue'
	import My from './components/my/my.vue'
	import Index from './components/index/index.vue'
	import User from './components/user/user.vue'

注册组件

components: {
	  Editor,
	  My,
	  Index,
	  User
  },

在data里面写入tabbarList, 具体配置参照uview的tabbarList

data() {
	return {
		current: 0 ,
		// 用户角色
		role: 'admin0',
		tabbarList:[
			[
				{
					title: 'my',
					name: 'My',
					iconPath: "home",
					selectedIconPath: "home-fill",
					customIcon: false,
					text: "我的",
				},
				{
					title: 'user',
					name: 'User',
					iconPath: "edit-pen",
					selectedIconPath: "edit-pen-fill",
					text: "user",
					customIcon: false,
				}
			],
			[
				{
					title: 'editor',
					name: 'Editor',
					iconPath: "home",
					selectedIconPath: "home-fill",
					customIcon: false,
					text: "编辑",
				},
				{
					title: 'Index',
					name: 'Index',
					iconPath: "edit-pen",
					selectedIconPath: "edit-pen-fill",
					text: "Index",
					customIcon: false,
				}
			],
		],
		tabbar: []
	}
}
// vue :is用法 动态组件的加载 不会的同学,可以去vue.js 学习
computed: {
	  component: function() {
		  return this.tabbar[this.current].name
	  }
  },
methods: {
// 根据用户角色加载tabbar,
	  getTabbar(){
		  switch(this.role) {
			  case 'user' :
			  this.tabbar = this.tabbarList[0]
			  break
			  case 'admin0':
			  this.tabbar = this.tabbarList[1]
		  }
	  },
	  changeTb(index){
		  this.current = index

			// 切换tabbar时,动态设置导航栏的文字
		  uni.setNavigationBarTitle({
		  	title: this.tabbar[index].name
		  })
	  }
  }
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
自定义 TabBar 切换时页面闪烁是因为页面重新渲染导致的,解决办法有很多种,这里介绍一种比较简单的做法。 1. 在 App.vue 中创建一个全局变量,用于记录当前选中的 Tab: ```javascript export default { globalData: { currentTab: 0, }, } ``` 2. 在 TabBar 组件中,绑定当前选中的 Tab: ```html <!-- TabBar.vue --> <template> <view class="tab-bar"> <view class="tab-item" :class="{ active: currentTab === 0 }" @click="switchTab(0)"></view> <view class="tab-item" :class="{ active: currentTab === 1 }" @click="switchTab(1)"></view> <view class="tab-item" :class="{ active: currentTab === 2 }" @click="switchTab(2)"></view> </view> </template> <script> export default { props: { // 从父组件传入当前选中的 Tab currentTab: { type: Number, default: 0, }, }, methods: { // 切换 Tab switchTab(index) { this.$emit('switchTab', index); }, }, }; </script> ``` 3. 在 TabBar 所在的页面中,监听 Tab 切换事件,并手动切换页面: ```html <template> <view> <view v-if="currentTab === 0">Tab 1</view> <view v-if="currentTab === 1">Tab 2</view> <view v-if="currentTab === 2">Tab 3</view> <TabBar :currentTab="currentTab" @switchTab="onSwitchTab" /> </view> </template> <script> export default { data() { return { currentTab: 0, }; }, methods: { // 监听 Tab 切换事件 onSwitchTab(index) { // 如果切换到当前页面,则不进行操作 if (index === this.currentTab) { return; } // 记录当前选中的 Tab this.currentTab = index; // 手动切换页面 uni.switchTab({ url: `/pages/tab${index + 1}/index`, }); }, }, }; </script> ``` 这样,在 Tab 切换时,页面不会重新渲染,也就不会出现闪烁的问题。需要注意的是,手动切换页面时,需要使用 `uni.switchTab` 方法,而不是 `uni.navigateTo` 或 `uni.redirectTo` 方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值