Vant 底部导航组件的实现 tabbar

本文探讨了如何利用Vant框架创建底部导航组件tabbar,涉及路由跳转的实现、选项卡的工作原理以及路由拦截等关键点。

设计底部导航的功能组件

1. 路由跳转

2. 选项卡的原理

3. 路由拦截

<template>
  <van-tabbar v-model="active" active-color="#30A3F8">
    <van-tabbar-item 
      v-for="(item,index) in tabbars" 
      :key="index" 
      :to="(item.name)"
    >
      <span>{{item.title}}</span>
      <img slot="icon" slot-scope="props" :src="props.active ? item.active : item.normal" />
    </van-tabbar-item>
  </van-tabbar>
</template>
<script>
export default {
  name: "tabbar",
  data() {
    return {
      active: 0,
      tabbars: [
        {
          name: "home",
          title: "首页",
          normal: require("@/assets/images/shop/icon1_1.png"),
          active: require("@/assets/images/shop/icon1_2.png")
        },
        {
          name: "sort",
          title: "分类",
          normal: require("@/assets/images/shop/icon2_1.png"),
          active: require("@/assets/images/shop/icon2_2.png")
        },
        {
          name: "cart",
          title: "购物车",
          normal: require("@/assets/images/shop/icon3_1.png"),
          active: require("@/assets/images/shop/icon3_2.png")
        },
        {
          name: "mine",
          title: "我的",
          normal: require("@/assets/images/shop/icon4_1.png"),
          active: require("@/assets/images/shop/icon4_2.png")
        }
      ]
    };
  },
  watch: {
    '$route'(to, from) {
      if (to.path == "/shop/home") {
          this.active = 0;
      } else if (to.path == "/shop/sort") {
          this.active = 1;
      } else if (to.path == "/shop/cart") {
          this.active = 2;
      } else if (to.path == "/shop/mine" || to.path == "/shop/myorder" || to.path == "/shop/myorderreturn") {
          this.active = 3;
      }
    }
  },
  created () {
    // console.log(this.$route);
    // if (this.$route.name == "首页") {
    //     this.active = 0;
    // } else if (this.$route.name == "分类") {
    //     this.active = 1;
    // } else if (this.$route.name == "购物车") {
    //     this.active = 2;
    // } else if (this.$route.name == "我的主页" || this.$route.name == "我的订单" || this.$route.name == "退款/售后") {
    //     this.active = 3;
    // }
  },
};
</script>

<style lang="less" scoped>
.van-tabbar {
  border-top: 1px solid #EEE;
  height: 55px;
  .van-tabbar-item__icon img {
    width: 22px;
    height: 22px;
  }
}

.active_tab img {
  width: 22px;
  height: 22px;
}
</style>

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_格鲁特宝宝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值