# Sidebar 侧边导航
垂直展示的导航栏,用于在不同的内容区域之间进行切换。
## 基础用法
通过 `v-model` 绑定当前选中项的索引。
```html
```
```typescript
const active = ref(0)
```
## 徽标提示
设置 `is-dot` 属性后,会在右上角展示一个小红点;设置 `badge` 属性后,会在右上角展示相应的徽标。
```html
```
## 禁用选项
通过 `disabled` 属性禁用选项。
```html
```
## 监听切换事件
设置 `change` 方法来监听切换导航项时的事件。
```html
```
```typescript
import { useToast } from '@/uni_modules/wot-design-uni'
const toast = useToast()
const active = ref(1)
function handleChange({ value, label }) {
toast.show(`当前标签名 ${label}`)
}
```
## 异步切换
通过 `before-change` 属性可以在切换标签前执行特定的逻辑。它接收 `{ value, resolve }` 参数,通过 `resolve` `继续执行,resolve` 接收 1 个 boolean 参数
```html
```
```typescript
import { useToast } from '@/uni_modules/wot-design-uni'
import type { SidebarBeforeChange } from '@/uni_modules/wot-design-uni/components/wd-sidebar/types'
import { ref } from 'vue'
const { loading: showLoading, close: closeLoading } = useToast()
const toast = useToast()
const active = ref(1)
const beforeChange: SidebarBeforeChange = ({ value, resolve }) => {
showLoading('切换中')
setTimeout(() => {
closeLoading()
resolve(true)
}, 2000)
}
```
## 锚点用法示例
sidebar 组件的锚点用法可以帮助用户在长页面上快速导航到特定的部分。
::: details 查看锚点用法示例
::: code-group
```html [vue]
```
```typescript [typescript]
```
```css [css]
.wraper {
display: flex;
height: calc(100vh - var(--window-top));
height: calc(100vh - var(--window-top) - constant(safe-area-inset-bottom));
height: calc(100vh - var(--window-top) - env(safe-area-inset-bottom));
}
.content {
flex: 1;
background: #fff;
}
```
:::
## 切换页面用法示例
sidebar 组件在每次切换激活项时,跳转到指定的页面,且无法通过滚动导航到下一个 sidebar 项。
::: details 查看切换页面用法示例
::: code-group
```html [vue]
```
```typescript [typescript]
```
```css [css]
.wraper {
display: flex;
height: calc(100vh - var(--window-top));
height: calc(100vh - var(--window-top) - constant(safe-area-inset-bottom));
height: calc(100vh - var(--window-top) - env(safe-area-inset-bottom));
overflow: hidden;
}
.content {
flex: 1;
background: #fff;
transition: transform 0.3s ease;
}
.category {
box-sizing: border-box;
height: 100%;
}
```
:::
## 自定义图标用法示例
设置`sidebar-item`的`icon`属性,自定义图标。
::: details 自定义图标用法示例
::: code-group
```html [vue]
```
```typescript [typescript]
```
```css [css]
.wraper {
display: flex;
height: calc(100vh - var(--window-top));
height: calc(100vh - var(--window-top) - constant(safe-area-inset-bottom));
height: calc(100vh - var(--window-top) - env(safe-area-inset-bottom));
}
.content {
flex: 1;
background: #fff;
}
```
:::
## Attributes
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------- | ---------------- | ------ | ------ | ---------------- |
| modelValue / v-model | 当前导航项的索引 | string / number | - | 0 | 0.1.49 |
| before-change | 切换导航项前钩子,可以在切换标签前执行特定的逻辑,接收 { value, resolve } 参数,通过 resolve 继续执行,resolve 接收 1 个 boolean 参数 | function | - | - | 1.4.0 |
## Events
| 事件名称 | 说明 | 参数 | 最低版本 |
| -------- | -------------- | ------------------------------------------ | -------- |
| change | 选项切换时触发 | `(value: number \| string, label: string)` | 0.1.49 |
## Slots
| name | 说明 | 参数 | 最低版本 |
| ------- | ---------- | ---- | -------- |
| default | 自定义展示 | - | 0.1.49 |
## 外部样式类
| 类名 | 说明 | 最低版本 |
| ----------- | ------------ | -------- |
| customStyle | 自定义样式 | 0.1.49 |
| customClass | 自定义样式类 | 0.1.49 |
## SidebarItem Attributes
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
| ----------- | ---------------------------------------------------------------------------------------- | -------------------------- | ------ | ------ | -------- |
| label | 当前选项标题 | string | - | - | 0.1.49 |
| value | 当前选项的值,唯一标识 | `number / string` | - | - | 0.1.49 |
| icon | 图标 | string | - | - | 0.1.49 |
| badge | 徽标属性,徽标显示值 | `number / string / null` | - | - | 0.1.49 |
| isDot | 徽标属性,是否点状徽标 | boolean | - | false | 0.1.49 |
| max | 徽标属性,徽标最大值 | number | - | 99 | 0.1.49 |
| disabled | 是否禁用 | boolean | - | false | 0.1.49 |
| badge-props | 自定义徽标的属性,传入的对象会被透传给 [Badge 组件的 props](/component/badge#attributes) | BadgeProps | - | - | 0.1.50 |
## SidebarItem Slots
| name | 说明 | 参数 | 最低版本 |
| ---- | ---------- | ---- | -------- |
| icon | 自定义图标 | - | 0.1.49 |
## SidebarItem 外部样式类
| 类名 | 说明 | 最低版本 |
| ----------- | ------------ | -------- |
| customStyle | 自定义样式 | 0.1.49 |
| customClass | 自定义样式类 | 0.1.49 |