vant 组件van-tabbar实现底部导航

文章介绍了如何在移动端H5开发中使用Vant框架的van-tabbar组件创建底部导航。首先,通过封装组件并引用到子页面来实现简单底部导航。然后,优化方法是创建主页面,结合路由配置,使van-tabbar在路由模式下工作,以实现更好的导航效果。文中提到,直接使用to属性可能导致首页高亮的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单使用van-tabbar实现底部导航

移动端小白,首次尝试移动H5开发,使用vant2的van-tabbar实现底部导航功能。本文忽略vant使用步骤,项目中使用全局引用。由于查询很多文章有的过于复杂,有的功能未实现,所以简单整理如有问题欢迎留言改正。

1、创建底部导航组件

底部导航栏简单实现,需要注意的是,组件是需要在所有需要使用的页面进行引用。所以将底部导航栏组合起来封装成一个组件。

// Tabbar组件
<template>
  <div>
  <router-view></router-view>
  <van-tabbar v-model="tabbarTempValue" route >
    <van-tabbar-item name="news" icon="home-o"  replace to="/news">资讯待办</van-tabbar-item>
    <van-tabbar-item name="workBench" icon="friends-o" replace to="/workBench">工作台</van-tabbar-item>
    <van-tabbar-item name="setting" icon="setting-o" replace to="/setting">我的</van-tabbar-item>
  </van-tabbar>
  </div>
</template>

<script>
export default {
  name: 'tabbar',
  props: {
    active: Number
  },
  data () {
    return {
      tabbarTempValue: this.active
    }
  },
  methods: {
  }
}
</script>

<style scoped>

</style>

2、子页面引用

// news页面
<template>
  <div>
    我是news
    <Tabbar :active=0 />
  </div>

</template>

<script>
import Tabbar from './tabbar'
export default {
  name: "news",
  components: {
    Tabbar
  },
  data () {
    return {}
  }
}
</script>

<style scoped>

</style>

优化van-tabbar使用

虽然上面的方法可以实现底部导航的功能,但是有点牵强。新的思路是创建主页面使用van-tabbar,开启路由模式,配合路由的层级结构实现底部的导航效果,完整的主要代码如下。

1、创建主页面

// homePage页面主页代码
<router-view />
    <van-tabbar class="layout-tabbar" route  >
      <van-tabbar-item name="news" icon="home-o"  :to="{name: 'news'}">资讯</van-tabbar-item>
      <van-tabbar-item name="workBench" icon="friends-o"   :to="{name : 'workBench'}">工作台</van-tabbar-item>
      <van-tabbar-item name="setting" icon="setting-o"   :to="{name : 'setting'}">设置</van-tabbar-item>
    </van-tabbar>

2、创建子模块

   子模块代码没有限制

3、配置路由(很重要)

{path: '/',
  component: _import('modules/mobile/homePage'),
  children: [
    {
      path: '', // 默认子路由
      name: 'news',
      component: _import('modules/mobile/news')
    },
    {
      path: '/setting', // 设置
      name: 'setting',
      component: _import('modules/mobile/setting')
    },
    {
      path: '/workBench', // 工作台
      name: 'workBench',
      component: _import('modules/mobile/workBench')
    }
  ]

4、总结

路由中配置子路由后开启van-tabbar 的路由模式可以实现底部导航的效果。需要注意van-tabbar 的to如果直接写成to="/seting"的模式会导致首页一直高亮(原因不明欢迎留言)。

### 如何在 UniApp 项目中使用 `van-tabbar` 组件 #### 安装 Vant Weapp 组件库 为了能够在 UniApp 中使用 `van-tabbar`,首先需要安装 Vant Weapp 组件库。可以通过 npm 或者 yarn 来完成这一步骤。 ```bash npm install @vant/weapp -D --production ``` 或者 ```bash yarn add @vant/weapp -D ``` #### 修改 `pages.json` 文件配置 TabBar 为了让应用程序支持底部标签栏,在项目的根目录下找到并编辑 `pages.json` 文件来设置全局的 tabBar 属性[^3]: ```json { "globalStyle": { ... }, "tabBar": { "color": "#000", "selectedColor": "#1aad19", "borderStyle": "black", "backgroundColor": "#fff", "list": [ { "pagePath": "pages/index/index", "text": "首页" }, { "pagePath": "pages/logs/logs", "text": "日志" } ] } } ``` #### 使用 `van-tabbar-item` 组件 接下来可以在对应的页面文件里引入 `van-tabbar-item` 并编写相应的代码片段以实现具体的功能逻辑[^1]: 对于每一个要显示为 tab 的页面(比如上面提到的 `index.vue` 和 `logs.vue`),都需要在其 `<template>` 标签内加入如下所示的内容: ```html <template> <!-- 其他 HTML 结构 --> <van-tabbar v-model="active"> <van-tabbar-item icon="home-o">首页</van-tabbar-item> <van-tabbar-item icon="logistics-o">物流</van-tabbar-item> </van-tabbar> <!-- 更多HTML结构 --> </template> <script> import '@vant/weapp/dist/style.css'; export default { data() { return { active: 0, }; }, }; </script> ``` 上述例子展示了如何通过绑定属性 `v-model` 控制当前激活项的状态,并利用子组件 `van-tabbar-item` 渲染各个选项卡的文字描述及其图标。 #### 初始化 App 设置 最后不要忘记调整 `App.vue` 文件中的启动函数以便更好地适配不同平台的需求[^4]: ```javascript onLaunch: function () { console.log('App Launch'); uni.getSystemInfo({ success(res) { // 获取状态栏高度等信息... // 如果是在微信小程序环境下执行特定操作 #ifdef MP-WEIXIN const menuRect = wx.getMenuButtonBoundingClientRect(); this.globalData.StatusBarHeight = res.statusBarHeight; this.globalData.CustomBarHeight = menuRect.bottom - res.statusBarHeight + (menuRect.top === 4 ? 0 : 8); #endif // 将这些值存储到 globalData 或其他合适的地方供后续访问 }.bind(this), }); }, ``` 这段脚本会根据运行环境自动计算出顶部安全区域的高度,从而确保自定义导航栏不会被遮挡。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值