Tabs Tabs
Switch page sections with scrollable headers and leave guards.
Import
Use <hui-tabs> 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.
vue
<template>
<view>
<hui-tabs
ref="control"
v-model="active"
:items="items"
@update:model-value="event0"
@change="event1"
@click-tab="event2"
><hui-tab-panel name="first"><text>概览内容 / Overview content</text></hui-tab-panel
><hui-tab-panel name="second"
><text>详情内容 / Details content</text></hui-tab-panel
></hui-tabs
>
<hui-button @click="() => control?.resize()">resize()</hui-button>
<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 control = ref()
const active = ref<string | number>('first')
const items = [
{ name: 'first', label: '概览 / Overview' },
{ name: 'second', label: '详情 / Details' },
{ name: 'disabled', 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(', ')
}
const event2 = (arg0: unknown, arg1: unknown) => {
lastEvent.value = 'click-tab' + ': ' + [describe(arg0), describe(arg1)].join(', ')
}
</script>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
modelValue | TabsName | undefined | Controlled value; undefined means omitted. |
defaultValue | TabsName | — | Initial uncontrolled value; ignored in controlled mode. |
items | TabsItem[] | [] | Item data. |
type | "line" | "card" | 'line' | Semantic type or input mode; see the type definition. |
size | ComponentSize | — | Component size; inherits configuration when omitted. |
scrollable | boolean | undefined | Enable horizontal tab scrolling; derive from item count when omitted. |
scrollThreshold | number | 5 | Enable horizontal tab scrolling by default above this item count. |
lineWidth | string | number | — | Tab indicator width. |
lineHeight | string | number | — | Tab indicator height. |
ellipsis | boolean | true | Truncate long tab titles. |
swipeable | boolean | false | Allow touch swipes to switch tabs; mouse dragging is not supported. |
lazyRender | boolean | true | Render content after its first activation or opening. |
beforeLeave | ((name: TabsName, oldName: TabsName | undefined) => unknown) | — | Tab leave guard; false retains the active tab. |
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: TabsName) | Request a controlled value update. |
change | (name: TabsName, ctx: { item: TabsItem; oldName?: TabsName }) | Emitted after the value changes. |
click-tab | (name: TabsName, ctx: { item: TabsItem; disabled: boolean }) | Reports the corresponding interaction result. |
Slots
| Slot | Parameters | Description |
|---|---|---|
nav-left | — | Custom nav-left region; replaces its fallback content. |
label | { item, active } | Custom label region; replaces its fallback content. |
nav-right | — | Custom nav-right region; replaces its fallback content. |
default | — | Main content. |
Methods
| Method | Signature | Description |
|---|---|---|
resize | (): Promise<void> | Measure tab titles and the indicator again. |
Types
TabsProps、TabsName、TabsItem、TabsExpose、TabsEmits are available from @nscode/handy-uni.
ts
export type TabsName = string | number
export interface TabsItem {
name: TabsName
label: string
disabled?: boolean
badge?: number | string
dot?: boolean
}
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.