介绍
垂直展示的导航栏,用于在不同的内容区域之间进行切换。
引入
在app.json或index.json中引入组件,详细介绍见快速上手。
"usingComponents": { "van-sidebar": "@vant/weapp/sidebar/index", "van-sidebar-item": "@vant/weapp/sidebar-item/index"}
Vant Weapp 1.0 版本开始支持此组件,升级方式参见快速上手
代码演示
基础用法
通过在van-sidebar上设置activeKey属性来控制选中项。
<van-sidebar active-key="{{ activeKey }}"> <van-sidebar-item title="标签名" /> <van-sidebar-item title="标签名" /> <van-sidebar-item title="标签名" /></van-sidebar>
Page({ data: { activeKey: 0, },});
徽标提示
设置dot属性后,会在右上角展示一个小红点。设置badge属性后,会在右上角展示相应的徽标。
<van-sidebar active-key="{{ activeKey }}"> <van-sidebar-item title="标签名" dot /> <van-sidebar-item title="标签名" badge="5" /> <van-sidebar-item title="标签名" badge="99+" /></van-sidebar>
禁用选项
通过disabled属性禁用选项。
<van-sidebar active-key="{{ activeKey }}"> <van-sidebar-item title="标签名" /> <van-sidebar-item title="标签名" disabled /> <van-sidebar-item title="标签名" /></van-sidebar>
监听切换事件
设置change方法来监听切换导航项时的事件。
<van-sidebar active-key="{{ activeKey }}" bind:change="onChange"> <van-sidebar-item title="标签名 1" /> <van-sidebar-item title="标签名 2" /> <van-sidebar-item title="标签名 3" /></van-sidebar><van-notify id="van-notify" />
import Notify from '@vant/weapp/dist/notify/notify';Page({ data: { activeKey: 0, }, onChange(event) { Notify({ type: 'primary', message: event.detail }); },});
API
| activeKey | 选中项的索引 | string | number | 0 | - |
|---|---|---|---|---|
| dot |
是否显示右上角小红点 |
boolean |
false |
|
|
|
|
| info | 图标右上角徽标的内容(已废弃,请使用 badge 属性) | string | number | ‘’ |
|---|---|---|---|
| badge v1.5.0 | 图标右上角徽标的内容 | string | number | ‘’ |
|---|---|---|---|
| disabled |
是否禁用该项 |
boolean |
false |
|
|
|
|
| title |
自定义标题栏,如果设置了title属性则不生效 |
|
|
| click |
点击徽章时触发 |
event.detail 为当前徽章的索引 |
|
|
|