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
      // });
相关推荐
夏幻灵14 分钟前
HTML5里最常用的十大标签
前端·html·html5
冰暮流星15 分钟前
javascript之二重循环练习
开发语言·javascript·数据库
Mr Xu_28 分钟前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
未来龙皇小蓝31 分钟前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions40 分钟前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发40 分钟前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
2501_9151063242 分钟前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
程序员猫哥_1 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞051 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
2501_915106321 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone