VantUI封装自定义Tabbar路由跳转的实现

本文主要介绍了VantUI封装自定义Tabbar路由跳转的实现,分享给大家,具体如下:

在这里插入图片描述

在src目录下新建个components文件夹来放自己定义的tabbar组件

<template>
<div
  v-if="
    $route.name == 'index' ||
      $route.name == 'learn' ||
      $route.name == 'person'
      "
>
  <van-tabbar v-model="active" inactive-color="#666666" active-color="#000000" fixed placeholder>
    <van-tabbar-item replace v-for="(item, index) in tabbarList" :key="index" :to="item.path">
      <span>{{ item.title }}</span>
      <img slot="icon" slot-scope="props" :src="props.active ? item.active : item.normal" />
    </van-tabbar-item>
  </van-tabbar>
</div>
</template>

<script>
export default {
name: "tabbar",
data() {
  return {
    active: 0,
    tabbarList: [
      {
        path: "/",
        title: "首页",
        normal:
          "https://sucai.suoluomei.cn/sucai_zs/images/20190910093117-fx2.png",
        active:
          "https://sucai.suoluomei.cn/sucai_zs/images/20190910093117-fx.png",
      },
      {
        path: "/learn",
        title: "学习",
        normal:
          "https://sucai.suoluomei.cn/sucai_zs/images/20190910093117-xx.png",
        active:
          "https://sucai.suoluomei.cn/sucai_zs/images/20190910093117-xx2.png",
      },
      {
        path: "/person",
        title: "我的",
        normal:
          "https://sucai.suoluomei.cn/sucai_zs/images/20190910093117-wd.png",
        active:
          "https://sucai.suoluomei.cn/sucai_zs/images/20190910093117-wd2.png",
      },
    ],
  };
},

//监听路由变化
watch: {
  $route(to) {
    this.activeTab(to.path);
  },
},

methods: {
  activeTab(path) {
    var index = this.tabbarList.map((item) => item.path).indexOf(path);
    if (index != -1) {
      this.active = index;
    }
  },
},
};
</script>

引入tabbar组件的页面到app.vue

<template>
<div id="app">
<router-view />
  <tabbar></tabbar>
</div>
</template>

<script>
import tabbar from "./components/tabbar.vue";  //引用组件的地址
export default {
components: {
  tabbar
},
name: "App",
data() {
  return {};
},
methods: {},

};
</script>

github完整项目

https://github.com/skywalk94/vueWechatH5

关于VantUI封装自定义Tabbar路由跳转的实现的文章就介绍至此,更多相关Vant封装Tabbar路由跳转内容请搜索编程宝库以前的文章,希望以后支持编程宝库

 前言代码地址开发前需要掌握的一些库koa:用来起一个web服务器koa2-cors: 解决跨域问题@koa/router: koa的路由处理koa-body: koa参数的 ...