Skip to content

TabBar 标签栏

更新: 2024/10/24 字数: 0 字 时长: 0 分钟

说明

van-tabbar 组件的二次封装,底部导航栏,用于在不同页面之间进行切换,具体可参考 Vant

基本使用

引入

App.vue文件中,组件已被自动注册,无需引入,详见unplugin-vue-components

vue
<template>
  <VanConfigProvider :theme="mode">
    <NavBar />
    <router-view v-slot="{ Component, route }">
      <transition :name="routeTransitionName">
        <keep-alive :include="keepAliveRouteNames">
          <component :is="Component" :key="route.name" />
        </keep-alive>
      </transition>
    </router-view>
    <TabBar />
    <ReDialog />
  </VanConfigProvider>
</template>

通过名称匹配

icon用的是iconify里的图标,可参考图标库,若用图片可选择activeinactive

vue
<TabBar v-model="activeName" :routes="routes" />
ts
import { ref } from "vue";

const activeName = ref(0);
const routes = ref([
  {
    title: "layouts.home",
    name: "home",
    icon: "i-carbon:home",
  },
  {
    title: "layouts.profile",
    name: "profile",
    icon: "i-carbon:user",
  },
]);

路由模式

需开启路由模式,页面跳转时to匹配路由里的路径

vue
<TabBar :routes="routes" is-route />
ts
import { ref } from "vue";

const routes = ref([
  {
    title: "layouts.home",
    name: "home",
    icon: "i-carbon:home",
    to: "/",
  },
  {
    title: "layouts.profile",
    name: "profile",
    icon: "i-carbon:user",
    to: "/profile",
  },
]);

API

传参配置

参数说明类型默认值
v-model当前选中标签的名称,需配合route里的name使用string-
routes路由对象,具体类型见下表Array<routeType>-
isRoute是否使用路由跳转,若开启,选中标签配置将不生效booleantrue
isReplace是否在跳转时替换当前页面历史booleanfalse
isFixed是否固定底部导航栏booleantrue
inActiveColor未选中标签颜色string#7d7e80
activeColor选中标签颜色string#1989fa
isBorder是否显示外边框booleantrue

routes详解

参数说明类型
title标签名称string
name标签的标识string
icon图标名称,配置后activeinactive不生效string
to点击后跳转的目标路由对象,等同于 Vue Router 的 to 属性string|object
url点击后跳转的目标链接地址string
active选中时的图片链接string
inactive未选中时的图片链接string
dot是否显示小红点boolean
badge图标右上角徽标的内容number|string
badgeProps自定义徽标组件的参数,传入的对象会透传给Badge 组件的 propsBadgeProps

类型声明

ts
interface routeType {
  title: string;
  name?: string;
  icon: string;
  to?: string;
  url?: string;
  active?: string;
  inactive?: string;
  dot?: boolean;
  badge?: number | string;
  badgeProps?: object;
}

Released under the MIT License.

本站访客数 人次 本站总访问量