Back to Vant Weapp

Sidebar 侧边导航

packages/sidebar/README.md

1.11.73.5 KB
Original Source

Sidebar 侧边导航

介绍

垂直展示的导航栏,用于在不同的内容区域之间进行切换。

引入

app.jsonindex.json中引入组件,详细介绍见快速上手

json
"usingComponents": {
  "van-sidebar": "@vant/weapp/sidebar/index",
  "van-sidebar-item": "@vant/weapp/sidebar-item/index"
}

Vant Weapp 1.0 版本开始支持此组件,升级方式参见快速上手

代码演示

基础用法

通过在van-sidebar上设置activeKey属性来控制选中项。

html
<van-sidebar active-key="{{ activeKey }}">
  <van-sidebar-item title="标签名" />
  <van-sidebar-item title="标签名" />
  <van-sidebar-item title="标签名" />
</van-sidebar>
javascript
Page({
  data: {
    activeKey: 0,
  },
});

徽标提示

设置dot属性后,会在右上角展示一个小红点。设置badge属性后,会在右上角展示相应的徽标。

html
<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属性禁用选项。

html
<van-sidebar active-key="{{ activeKey }}">
  <van-sidebar-item title="标签名" />
  <van-sidebar-item title="标签名" disabled />
  <van-sidebar-item title="标签名" />
</van-sidebar>

监听切换事件

设置change方法来监听切换导航项时的事件。

html
<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" />
js
import Notify from '@vant/weapp/notify/notify';

Page({
  data: {
    activeKey: 0,
  },

  onChange(event) {
    Notify({ type: 'primary', message: event.detail });
  },
});

API

参数说明类型默认值
activeKey选中项的索引string | number0
事件名说明参数
change切换徽章时触发当前选中徽章的索引
类名说明
custom-class根节点样式类

SidebarItem Props

参数说明类型默认值
title内容string''
dot是否显示右上角小红点booleanfalse
info图标右上角徽标的内容(已废弃,请使用 badge 属性)string | number''
badge v1.5.0图标右上角徽标的内容string | number''
disabled是否禁用该项booleanfalse

SidebarItem Slot

名称说明
title自定义标题栏,如果设置了title属性则不生效

SidebarItem Event

事件名说明参数
click点击徽章时触发event.detail 为当前徽章的索引

SidebarItem 外部样式类

类名说明
custom-class根节点样式类