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>

 

 
posted @ 2020-09-01 17:40  格鲁特baby  阅读(3648)  评论(0编辑  收藏  举报