Sidebar 侧边导航
数据驱动的侧边导航;快速切换时只接受最后一次守卫结果。
引入
uni-app 配置 easycom 后使用 <hui-sidebar>。公开类型和函数从 @nscode/handy-uni 导入;普通 Vue 3 安装 @nscode/handy-uni/web 预览插件。
示例
示例可独立运行;交互结果直接显示在页面。
vue
<template>
<view>
<hui-sidebar
v-model="selected"
:items="items"
:height="240"
:before-change="beforeChange"
@change="onChange"
@click="onClick"
>
<template #default="{ active }"
><text>当前内容 / Current content: {{ active }}</text></template
> </hui-sidebar
><text>{{ selected }} / {{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { SidebarName, SidebarItem } from '@nscode/handy-uni'
const lastEvent = ref('尚无事件 / No event')
const selected = ref<string | number>('all')
const onChange = (name: SidebarName, context: { item: SidebarItem; oldName?: SidebarName }) => {
lastEvent.value = JSON.stringify({ name, context })
}
const onClick = (item: SidebarItem, context: { index: number }) => {
lastEvent.value = JSON.stringify({ item, context })
}
const items = [
{ name: 'all', label: '全部 / All' },
{ name: 'favorite', label: '收藏 / Favorites', badge: 3 },
{ name: 'disabled', label: '禁用 / Disabled', disabled: true }
]
const beforeChange = async () => true
</script>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
modelValue | SidebarName | — | 当前项的 name |
defaultValue | SidebarName | — | 非受控初始值;不给时取第一个可用项 |
items | SidebarItem[] | [] | 条目数据。 |
beforeChange | ( name: SidebarName, oldName: SidebarName | undefined ) => boolean | void | Promise<boolean | void> | — | latest 模式(和 TabBar、Tabs 一致) |
width | number | string | 88 | 导航列宽度,数字按 px |
height | number | string | — | 整体高度;不传时撑满父容器(父容器要有高度) |
label | string | — | 读屏名称;不传用 t('sidebar.label') |
根节点类名和样式使用 customClass、customStyle。尺寸数字按 px,时长除明确说明外按 ms。
Events
| 事件 | 参数 |
|---|---|
update:modelValue | (name: SidebarName) |
change | (name: SidebarName, context: { item: SidebarItem; oldName?: SidebarName }) |
click | (item: SidebarItem, context: { index: number }) |
受控状态由父级回写更新事件后改变。多词事件使用短横线。禁用操作、取消及过期异步结果不提交成功变更。
Slots
| 插槽 | 作用域 |
|---|---|
item | { item, active } |
default | { active } |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
scrollToActive | (): Promise<void> | 把当前导航项滚入视口 |
类型
SidebarName、SidebarItem、SidebarProps、SidebarEmits、SidebarExpose.
平台说明
基于共享平台组件实现 H5、微信小程序、App-vue 与普通 Vue 3 Web。单元测试不代替设备验收;触摸、读屏、页面失活和原生组件表现按冒烟清单逐端核对。
设备冒烟清单.