TabBar Tab bar
Switch primary pages with optional routing and guarded changes.
Import
Use <hui-tab-bar> with uni-app easycom. Import public types from @nscode/handy-uni. For ordinary Vue 3, install the preview plugin from @nscode/handy-uni/web and register its components.
Examples
Interact with the controls to see values and event results on the page.
<template>
<view>
<hui-tab-bar
v-model="active"
:items="items"
:fixed="false"
@update:model-value="event0"
@change="event1"
/>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('No event yet')
const describe = (value: unknown) => {
try {
return JSON.stringify(value) ?? String(value)
} catch {
return String(value)
}
}
const active = ref<string | number>('home')
const items = [
{ name: 'home', label: '首页 / Home', icon: 'home' },
{ name: 'settings', label: '设置 / Settings', icon: 'setting', badge: 3 },
{ name: 'locked', label: '禁用 / Disabled', disabled: true }
]
const event0 = (arg0: unknown) => {
lastEvent.value = 'update:modelValue' + ': ' + [describe(arg0)].join(', ')
}
const event1 = (arg0: unknown, arg1: unknown) => {
lastEvent.value = 'change' + ': ' + [describe(arg0), describe(arg1)].join(', ')
}
</script>
Return false or reject beforeChange to prevent switching. Route mode uses the configured router adapter. Keep a placeholder for fixed bars to avoid obscuring content.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
modelValue | TabBarName | undefined | Controlled value; undefined means omitted. |
defaultValue | TabBarName | — | Initial uncontrolled value; ignored in controlled mode. |
items | TabBarItem[] | [] | Item data. |
fixed | boolean | true | Use fixed positioning. |
placeholder | boolean | true | Placeholder when no value is selected or entered. |
safeAreaInsetBottom | boolean | true | Reserve the bottom safe area. |
border | boolean | true | Show a border or separator. |
activeColor | string | — | Color of the active item. |
inactiveColor | string | — | Color of inactive items. |
beforeChange | ((name: TabBarName, oldName: TabBarName | undefined) => unknown) | — | Change guard; duplicate changes are ignored while pending. |
route | boolean | false | Navigate through the router adapter after selecting a tab. |
zIndex | number | 10 | Layer z-index; allocated from the current page stack when omitted. |
Root attributes: common props. A Boolean default of undefined supports inheritance; explicit false overrides it. Other Boolean props default to false unless listed otherwise.
Events
| Event | Parameters | Description |
|---|---|---|
update:modelValue | (name: TabBarName) | Request a controlled value update. |
change | (name: TabBarName, ctx: { item: TabBarItem; oldName?: TabBarName }) | Emitted after the value changes. |
Slots
| Slot | Parameters | Description |
|---|---|---|
icon | { item, active } | Custom icon region; replaces its fallback content. |
label | { item, active } | Custom label region; replaces its fallback content. |
Methods
None.
Types
TabBarProps、TabBarItem、TabBarName、TabBarEmits are available from @nscode/handy-uni.
export type TabBarName = string | number
export interface TabBarItem {
name: TabBarName
label: string
icon?: string
activeIcon?: string
badge?: number | string
dot?: boolean
disabled?: boolean
url?: string
}
Platform notes
| Behavior | uni-app H5 | WeChat Mini Program | App-vue | Ordinary Vue 3 Web (preview) |
|---|---|---|---|---|
| Basic interaction | Supported; device checks pending | Supported; device checks pending | Supported; device checks pending | Supported by the web platform adapter |
Follow the smoke checklist. Device evidence is tracked separately; unit tests do not establish device acceptance.