uni-app自定义tabbar(van-tabbar)

main.vue

js 复制代码
<template>
	<view>
		<index v-if="currentIndex === 0"></index>
		<signup v-else-if="currentIndex === 1"></signup>
		<test v-else-if="currentIndex === 2"></test>
		<custom-tabbar ref="tabBarComponent" @changeItem="changeItem"></custom-tabbar>
	</view>
</template>

<script>
	import signup from './signup/signup.vue'
	import test from './test.vue'
	import index from './index.vue'

	export default {
		components: {
			signup,
			test,
			index
		},
		data() {
			return {
				currentIndex: 0
			}
		},
		methods: {
			changeItem(item) {
				this.currentIndex = item
			}
		}
	}
</script>

<style scoped>
page{
			background-color: #f6f7fb;
		}
</style>

custom-tabbar

js 复制代码
<template>
  <view>
    <van-tabbar :active="active" @change="onChange" :placeholder="true"  active-color="#3856ff" inactive-color="#666666">
      <van-tabbar-item v-for="(item, index) in list" :key="index" :icon="item.icon">
        {{ item.text }}
      </van-tabbar-item>
    </van-tabbar>
  </view>
</template>

<script>
export default {
  data() {
    return {
      active: 0,
      list: [
        {
          icon: 'home-o',
          text: '首页',
          url: '/pages/index'
        },
        {
          icon: 'search',
          text: '示例2',
          url: '/pages/signup/signup'
        },
        {
          icon: 'smile-o',
          text: '示例3',
          url: '/pages/test'
        }
      ]
    };
  },

  methods: {
    onChange(event) {
      this.active = event.detail;
	  this.$emit("changeItem",this.active);
      // uni.switchTab({
      //   url: this.list[event.detail].url
      // });
    },

    init() {
      const pages = getCurrentPages();
      const page = pages[pages.length - 1];
      this.active = this.list.findIndex(item => item.url === `/${page.route}`);
    }
  },

  mounted() {
    // this.init();
  }
};
</script>

全部菜单放在一个界面使用v-if显示隐藏的做法是因为单独页面的话第一次切换下标菜单栏会闪烁(但是使用v-if又要自定义导航栏,就...挺离谱的), 如果不使用这种v-if显示的话(单独页面)可以在每个菜单界面加上以下代码(会闪烁),如果大家有更好的方案可以交流一下

js 复制代码
		<!-- 引入组件,并添加 ref 属性 -->
		<custom-tabbar ref="tabBarComponent"></custom-tabbar>
		
		onShow() {
			this.$refs.tabBarComponent.init();
		},
		// 解除下面的注释
		// uni.switchTab({
      //   url: this.list[event.detail].url
      // });
相关推荐
苏琢玉几秒前
从 Hexo 到 Astro:重构我的个人博客
前端·hexo
Glommer3 分钟前
某音 Js 逆向思路
javascript·逆向
街尾杂货店&7 分钟前
webpack - 单独打包指定JS文件(因为不确定打出的前端包所访问的后端IP,需要对项目中IP配置文件单独拿出来,方便运维部署的时候对IP做修改)
前端·javascript·webpack
月光技术杂谈9 分钟前
用Deepseek 实现一个基于web的扣图应用
前端·javascript·html5·ccs·tensorflow.js·canvas api
Q_Q51100828536 分钟前
python+uniapp基于微信小程序的学院设备报修系统
spring boot·python·微信小程序·django·flask·uni-app
金梦人生1 小时前
Css性能优化
前端·css
Holin_浩霖1 小时前
UI设计的底层逻辑:从组件到系统的跃迁
前端
Holin_浩霖1 小时前
前端开发者的 Web3 全图解实战 二
前端
写代码的皮筏艇1 小时前
CSS属性继承与特殊值
前端·css
kevlin_coder1 小时前
🚀 实现同一个滚动区域包含多个虚拟滚动列表
前端·javascript