VantUi 底部Tabbar跳转页面的方法以及产生的Bug问题

第一种方法:使用计算属性(computed)进行(if判断 / switch case)

需要加上get()和set(),不然报错

<template>
  <div>
    <van-tabbar v-model="active" active-color="darkred" inactive-color="#666">
      <van-tabbar-item icon="home-o" to="/home">首页</van-tabbar-item>
      <van-tabbar-item icon="label-o" to="/topic">专题</van-tabbar-item>
      <van-tabbar-item icon="apps-o" to="/category">分类</van-tabbar-item>
      <van-tabbar-item icon="cart-o" to="cart">购物车</van-tabbar-item>
      <van-tabbar-item icon="user-o" to="/user">我的</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>

export default {
  // data() {
  //   return {
  //     active: 0,
  //   };
  // },
  //通过这个获取到网页的路径 ps:this.$route(只读),this.$router(跳转)
  mounted() {
    console.log(this.$route.path);
  },
    computed: {
    active: {
      get(){
        switch (this.$route.path) {
        case "/wwyx":
          return 0;
        case "/toptic":
          return 1;
        case "/category":
          return 2;
        case "/cart":
          return 3;
        case "/user":
          return 4;
        default:
          break;
      }
      },
      set(){}
    }
  }
  
};
</script>

<style lang="scss" scoped>
</style>

第二种方法:通过在路由加上meta属性

在这里插入图片描述
设置对应的meta的num属性

//在页面上获取到路由meta上的num值

  mounted() {
     console.log(this.$route.meta.num);
  },
//把v-model换成$route.meta.num
<template>
  <van-tabbar v-model="$route.meta.num" v-if="$route.meta.isShowTabbar">
    <van-tabbar-item to="/wyyx" icon="home-o">首页</van-tabbar-item>
    <van-tabbar-item to="/toptic" icon="bookmark-o">专题</van-tabbar-item>
    <van-tabbar-item to="/category" icon="apps-o">分类</van-tabbar-item>
    <van-tabbar-item to="/cart" icon="shopping-cart-o">购物车</van-tabbar-item>
    <van-tabbar-item to="/user" icon="user-o">我的</van-tabbar-item>
  </van-tabbar>
</template>

这个方法存在bug,会出现tabbar出现乱跳转的问题,推荐还是用switch的方法。

点击跳转的时候控制台报错产生的这个问题 ,重复的重定向引起vue-router报错。。。

在这里插入图片描述
解决办法有两种:
1、在router里的index.js加上这一段代码:

// 解决Vue-Router升级导致的Uncaught(in promise) navigation guard问题

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location, onResolve, onReject) {

  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)

  return originalPush.call(this, location).catch(err => err)

}

2、删除 node_modules ,到 package.json 中将 vue-router 改为 3.0.7 ,重新 npm i 即可。

### 使用 Vant Tabbar 实现点击跳转功能 在微信小程序开发中,利用 `Vant` 提供的 `Tabbar` 和 `TabbarItem` 组件可以轻松实现底部导航栏,并支持页面间的跳转。以下是具体实现方式: #### 自定义 Tabbar 配置 为了使 Tabbar 支持动态切换和页面跳转,需先配置自定义组件接管默认的 Tabbar 渲染逻辑。 ##### WXML 文件 (custom-tab-bar/index.wxml) ```html <van-tabbar active="{{ active }}" bind:change="onChange"> <van-tabbar-item wx:for="{{ list }}" wx:key="index" icon="{{ item.icon }}"> {{item.text}} </van-tabbar-item> </van-tabbar> ``` 上述代码展示了如何通过循环渲染多个 Tabbar Item 并绑定 `bind:change` 方法来监听用户的点击行为[^1]。 ##### JS 文件 (custom-tab-bar/index.js) ```javascript Component({ data: { active: 0, list: [ { text: &#39;首页&#39;, icon: &#39;home-o&#39; }, { text: &#39;分类&#39;, icon: &#39;apps-o&#39; }, { text: &#39;购物车&#39;, icon: &#39;cart-o&#39; }, { text: &#39;我的&#39;, icon: &#39;user-o&#39; } ] }, methods: { onChange(event) { const index = event.detail; this.setData({ active: index }); // 获取目标路径并执行页面跳转 const pages = [&#39;pages/home/home&#39;, &#39;pages/category/category&#39;, &#39;pages/cart/cart&#39;, &#39;pages/user/user&#39;]; const url = pages[index]; wx.switchTab({ url }); // 切换到对应 tab 页面 } } }); ``` 此部分实现了当用户点击某个 Tabbar Item 后触发 `onChange` 函数,函数内部会更新当前激活项索引 (`active`) 并调用 `wx.switchTab` 完成页面跳转。 #### 动态图标显示 如果希望在选中状态时更换图标样式,则可以通过判断 `active` 属性的状态来进行调整。 ##### 修改后的 WXML 文件 ```html <van-tabbar active="{{ active }}" bind:change="onChange"> <block wx:for="{{ list }}" wx:key="index"> <van-tabbar-item custom-class="tabbar-item" info="{{ item.info || &#39;&#39; }}" dot="{{ !!item.dot }}" badge="{{ item.badge || null }}" @click="onItemClick(index)"> <!-- 图标 --> <view slot="icon" class="slot-icon"> <image src="{{ item.activeIcon ? item.activeIcon : item.icon }}"></image> </view> <!-- 文本 --> <text>{{ item.text }}</text> </van-tabbar-item> </block> </van-tabbar> ``` 此处引入了新的属性 `@click` 来捕获单击事件,并允许开发者更灵活地控制图标的加载逻辑[^2]。 ##### 更新后的数据结构 ```javascript data: { active: 0, list: [ { text: &#39;首页&#39;, icon: &#39;/images/tab_home.png&#39;, activeIcon: &#39;/images/tab_home_active.png&#39; }, { text: &#39;分类&#39;, icon: &#39;/images/tab_category.png&#39;, activeIcon: &#39;/images/tab_category_active.png&#39; }, { text: &#39;购物车&#39;, icon: &#39;/images/tab_cart.png&#39;, activeIcon: &#39;/images/tab_cart_active.png&#39; }, { text: &#39;我的&#39;, icon: &#39;/images/tab_user.png&#39;, activeIcon: &#39;/images/tab_user_active.png&#39; } ] } ``` 以上修改使得每个选项卡都可以拥有独立的未选中与已选中的图片资源链接。 --- ### 总结 综上所述,在微信小程序项目里借助于 `Vant Weapp` 中的 `Tabbar` 及其子组件能够快速搭建起具备交互能力的应用底边菜单条;同时配合小程序 API 如 `switchTab()` 即可达成跨页浏览的需求[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值