Skip to content

SideBar 侧边导航

侧边导航栏,用于在不同的内容区域之间进行切换。

导入

js
import SideBar from '@/components/nav/SideBar.vue';
import SideBarItem from '@/components/nav/SideBarItem.vue';

使用方法

基础用法

SideBar 组件需要与 SideBarItem 组件配合使用,通过 v-model 绑定选中的条目。

vue
<template>
  <SideBar v-model="value">
    <SideBarItem name="first" text="选项1" />
    <SideBarItem name="second" text="选项2" />
    <SideBarItem name="third" text="选项3" />
    <SideBarItem name="fourth" text="选项4" />
  </SideBar>
</template>

<script setup>
import { ref } from 'vue';
import SideBar from '@/components/nav/SideBar.vue';
import SideBarItem from '@/components/nav/SideBarItem.vue';

const value = ref('first');
</script>

徽标提示

通过 SideBarItem 的 badge 属性可以添加徽标提示,支持数字、红点等形式。

vue
<template>
  <SideBar v-model="value">
    <SideBarItem name="first" text="选项1" />
    <SideBarItem name="second" text="选项2" :badge="-1" /> <!-- 红点提示 -->
    <SideBarItem name="third" text="选项3" :badge="50" /> <!-- 数字提示 -->
    <SideBarItem name="fourth" text="选项4" :badge="100" :badgeProps="{ maxCount: 99 }" /> <!-- 自定义最大值 -->
  </SideBar>
</template>

禁用选项

通过 SideBarItem 的 touchable 属性可以禁用特定选项,使其不可点击。

vue
<template>
  <SideBar v-model="value">
    <SideBarItem name="first" text="选项1" />
    <SideBarItem name="second" text="选项2" :touchable="false" />
    <SideBarItem name="third" text="选项3" :touchable="false" />
    <SideBarItem name="fourth" text="选项4" />
  </SideBar>
</template>

监听切换事件

通过监听 SideBar 的 clickItem 事件,可以在用户切换选项时执行自定义逻辑。

vue
<template>
  <SideBar v-model="value" @clickItem="onItemClick">
    <SideBarItem name="first" text="选项1" />
    <SideBarItem name="second" text="选项2" />
    <SideBarItem name="third" text="选项3" />
    <SideBarItem name="fourth" text="选项4" />
  </SideBar>
</template>

<script setup>
import { ref, watch } from 'vue';

const value = ref('first');

watch(value, (newValue, oldValue) => {
  console.log('选中项目更改', newValue);
});
function onItemClick(name) {
  console.log('用户点击了', name);
}
</script>

API 参考

名称说明类型必填默认值
v-model选中的条目名称string | number''
activeColor选中文字颜色string'primary'
inactiveColor未选中文字颜色string'text.title'
disableColor禁用文字颜色string'text.second'
activeStyle选中时条目的样式object{}
inactiveStyle未选中时条目的样式object{}
activeBadgeStyle选中时条目左侧的标记的样式object{}
textStyle标签文字样式object{}
innerStyle自定义外层样式object{}
名称说明参数
update:modelValue选中项变化时触发name: string | number
clickItem点击菜单项时触发name: string | number
名称说明
default侧边栏内容,通常放置 SideBarItem 组件
background背景内容插槽

SideBarItem

SideBarItem Props

名称说明类型必填默认值
name标签名称,必填,请保证组件内不重复string | number-
text标签文字string-
touchable是否可以点击booleantrue
badge标签标记。为 0 或者 未定义时不显示,为 -1时显示圆点,为大于0的数时显示数字标记numberundefined
badgeProps自定义徽标的属性,传入的对象会被透传给 Badge 组件的 propsobject{}
textStyle标签文字样式object{}
innerStyle自定义标签样式object{}

SideBarItem Slots

名称说明
text自定义渲染文字内容

主题变量

名称类型默认值说明
SideBarItemTextFontSizenumber28侧边栏文字大小
SideBarItemPaddingVerticalnumber36侧边栏项垂直内边距
SideBarItemPaddingHorizontalnumber30侧边栏项水平内边距
SideBarItemActiveBackgroundColorstring'white'选中项背景颜色
SideBarItemInactiveBackgroundColorstring'light'未选中项背景颜色
SideBarItemActiveBadgeBackgroundColorstring'primary'选中标记背景颜色
SideBarItemActiveBadgeLeftnumber0选中标记左侧位置
SideBarItemActiveBadgeTopstring'50%'选中标记顶部位置
SideBarItemActiveBadgeMarginTopnumber-15选中标记上外边距
SideBarItemActiveBadgeHeightnumber30选中标记高度
SideBarItemActiveBadgeWidthnumber8选中标记宽度