Skip to content

AdminLayout

主布局组件,支持侧边栏、顶部导航、混合模式三种布局方式。

基础用法

vue
<script setup>
import AdminLayout from 'vue3-admin-layout'
import 'vue3-admin-layout/dist/style.css'

const menuOptions = [
  { key: 'home', label: '首页' },
  { key: 'about', label: '关于' },
]
</script>

<template>
  <AdminLayout :menu-options="menuOptions">
    <div>内容区域</div>
  </AdminLayout>
</template>

布局模式

通过 mode prop 切换三种布局模式:

拆分菜单

通过 splitMenu prop 启用拆分菜单模式,左侧显示一级菜单图标,右侧显示子菜单:

属性 Props

基础配置

属性名类型默认值说明
mode'side' | 'mix' | 'top''side'布局模式
splitMenubooleanfalse是否拆分菜单(仅在 side/mix 模式下有效)
siderCollapsedbooleanfalse侧边栏是否折叠
isMobilebooleanfalse是否移动端模式
scrollbarPropsScrollbarPropsundefined滚动条配置,详见 ScrollbarProps

Logo 配置

属性名类型默认值说明
logobooleantrue是否显示 Logo
logoUrlstringundefinedLogo 图片 URL
titlestring''标题文字

菜单配置

属性名类型默认值说明
menuOptionsMenuOption[][]菜单配置数组,label 支持字符串或函数
accordionbooleanfalse是否手风琴模式
activeKeystringundefined当前激活的菜单项 key

头部配置

属性名类型默认值说明
headerbooleantrue是否显示头部
headerHeightnumber48头部高度(像素)
headerThemestringundefined头部主题颜色
headerFixedbooleantrue头部是否固定
headerBorderedbooleantrue头部是否显示底部边框

侧边栏配置

属性名类型默认值说明
siderbooleantrue是否显示侧边栏
siderWidthnumber200侧边栏宽度(像素)
siderCollapsedWidthnumber48侧边栏折叠宽度(像素)
siderThemestringundefined侧边栏主题颜色
siderShowTriggerbooleantrue是否显示折叠触发按钮
siderRightFixedbooleantrue拆分菜单右侧是否固定(仅 side + splitMenu 模式)
siderBorderedbooleantrue侧边栏是否显示右边框
skinstringundefined毛玻璃皮肤背景图片 URL,设置后启用毛玻璃效果

内容区域配置

属性名类型默认值说明
contentFullbooleanfalse内容区域全屏模式
contentEmbeddedbooleantrue是否启用嵌入式内容背景色
contentWidthstring | number'100%'内容区域宽度(数字自动转为 px
contentHeaderbooleantrue是否显示内容头部(需配合 content-header 插槽)
contentHeaderHeightnumber36内容头部高度(像素)
contentHeaderFixedbooleantrue内容头部是否固定
contentHeaderBorderedbooleanfalse内容头部是否显示底部边框
contentFooterbooleantrue是否显示内容底部(需配合 content-footer 插槽)
contentFooterHeightnumber32内容底部高度(像素)
contentFooterFixedbooleanfalse内容底部是否固定

事件 Events

事件名参数说明
update:siderCollapsed(value: boolean)侧边栏折叠状态变化时触发
update:siderRightFixed(value: boolean)拆分菜单右侧固定状态变化时触发

插槽 Slots

主要插槽

插槽名参数说明
defaultAdminLayoutContentProps主要内容区域
logoAdminLayoutLogoProps自定义 Logo 区域
headerAdminLayoutHeaderProps头部区域(完全覆盖默认头部)
header-prefixAdminLayoutHeaderProps头部前缀区域(Logo 之后)
header-suffixAdminLayoutHeaderProps头部后缀区域

侧边栏插槽

插槽名参数说明
siderAdminLayoutSiderProps侧边栏主区域(完全覆盖)
sider-headerAdminLayoutSiderProps侧边栏头部
sider-contentAdminLayoutSiderProps侧边栏内容(优先级大于内置菜单)
sider-footerAdminLayoutSiderProps侧边栏底部
sider-leftAdminLayoutSiderProps拆分菜单左侧区域(完全覆盖)
sider-left-headerAdminLayoutSiderProps拆分菜单左侧头部
sider-left-contentAdminLayoutSiderProps拆分菜单左侧内容
sider-left-footerAdminLayoutSiderProps拆分菜单左侧底部
sider-rightAdminLayoutSiderProps拆分菜单右侧区域(完全覆盖)
sider-right-headerAdminLayoutSiderProps拆分菜单右侧头部
sider-right-contentAdminLayoutSiderProps拆分菜单右侧内容
sider-right-footerAdminLayoutSiderProps拆分菜单右侧底部

内容区域插槽

插槽名参数说明
content-headerAdminLayoutContentProps内容区域头部
content-footerAdminLayoutContentProps内容区域底部
content-overlayAdminLayoutContentProps内容区域遮罩层

菜单插槽

插槽名参数说明
menuAdminLayoutMenuProps自定义主菜单(替换内置菜单)
parent-menuAdminLayoutMenuProps自定义父级菜单(拆分模式下替换左侧菜单)

类型定义

ts
interface MenuOption {
  key?: string
  label?: string | ((option: MenuOption) => string)
  icon?: Component | string // Vue 组件或 iconify 图标字符串
  children?: MenuOption[]
  [key: string]: any
}

label 支持函数形式,可根据菜单项动态生成文本:

ts
const menuOptions: MenuOption[] = [
  { key: 'home', label: '首页' },
  { key: 'user', label: opt => `用户: ${opt.key}` },
]

ScrollbarProps

ts
interface ScrollbarProps {
  autoHide?: boolean // 是否自动隐藏滚动条,默认 true
  xScrollable?: boolean // 是否支持横向滚动,默认 false
  nativeScrollbar?: boolean // 是否使用原生滚动条,默认 false
  size?: number // 滚动条宽度(像素),默认 5
  height?: string // 容器高度
}

AdminLayoutInstance

ts
interface AdminLayoutInstance {
  state: AdminLayoutState
  toggleContentFull: (value: boolean) => void
  toggleSiderRightFixed: (value?: boolean) => void
  toggleSiderCollapsed: (value?: boolean) => void
}

AdminLayoutHeaderProps

ts
interface AdminLayoutHeaderProps {
  state: AdminLayoutState
  inverted: boolean
  height: number
  _height: number
  fixed: boolean
  bordered: boolean
  theme: string
  show: boolean
}

AdminLayoutSiderProps

ts
interface AdminLayoutSiderProps {
  state: AdminLayoutState
  show: boolean
  fixed: boolean
  theme: string
  width: number
  _width: number
  collapsedWidth: number
  _collapsedWidth: number
  inverted: boolean
  headerHeight: number
  collapsed: boolean
  toggleCollapsed: (value: boolean) => void
  toggleRightFixed: (value: boolean) => void
}

AdminLayoutContentProps

ts
interface AdminLayoutContentProps {
  state: AdminLayoutState
  height: number
  width: number
  scrollHeight: string
  contentWidth: string
}

AdminLayoutLogoProps

ts
interface AdminLayoutLogoProps {
  state: AdminLayoutState
  width: number
  height: number
  inverted: boolean
  collapsed: boolean
}

AdminLayoutMenuProps

ts
interface AdminLayoutMenuProps extends MenuProps {
  state: AdminLayoutState
}

interface MenuProps {
  collapsed?: boolean
  options?: any[]
  mode?: 'vertical' | 'horizontal'
  accordion?: boolean
  collapsedWidth?: number
  inverted?: boolean
  value?: string
  [key: string]: any
}

组件实例

通过 ref 可以访问组件实例和内部状态:

vue
<script setup>
import type { AdminLayoutInstance } from 'vue3-admin-layout'
import { ref } from 'vue'

const layoutRef = ref<AdminLayoutInstance>()

function toggleSidebar() {
  layoutRef.value?.toggleSiderCollapsed()
}

function toggleContentFull() {
  layoutRef.value?.toggleContentFull(true)
}

function toggleSiderRightFixed() {
  layoutRef.value?.toggleSiderRightFixed()
}
</script>

<template>
  <AdminLayout ref="layoutRef" :menu-options="menuOptions">
    <button @click="toggleSidebar">
      折叠/展开
    </button>
    <button @click="toggleContentFull">
      全屏
    </button>
    <button @click="toggleSiderRightFixed">
      固定/取消固定
    </button>
  </AdminLayout>
</template>

state 上可用的属性和方法:

属性/方法类型说明
siderCollapsedboolean侧边栏折叠状态
siderRightFixedboolean拆分菜单右侧固定状态
contentFullboolean内容全屏状态
isDarkboolean是否暗黑模式
modestring当前布局模式
isMobileboolean是否移动端
headerHeightnumber头部高度
siderWidthnumber侧边栏宽度
toggleSiderCollapsed(value?)(value?: boolean) => void切换侧边栏折叠
toggleSiderRightFixed(value?)(value?: boolean) => void切换拆分菜单右侧固定
toggleContentFull(value)(value: boolean) => void切换内容全屏

注意事项

  1. 样式导入:使用时必须导入样式文件 import 'vue3-admin-layout/dist/style.css'
  2. 暗黑模式:组件通过内置的 useDark() 自动检测暗黑主题,配合 @vueuse/core 使用即可。
  3. 路由集成:通过 activeKey 绑定当前路由路径,配合 Vue Router 实现菜单联动。
  4. 拆分菜单splitMenu 仅在 sidemix 模式下生效。
  5. 毛玻璃皮肤skin 设置背景图后,侧边栏和头部自动应用毛玻璃效果;暗黑模式下自动禁用。
  6. 边框控制headerBorderedsiderBorderedcontentHeaderBordered 可分别控制各区域的边框显示。

基于 MIT 许可发布