Skip to content

布局系统

isdream-vue-admin 提供两种布局模式:左侧菜单布局和顶部菜单布局。

布局模式

布局配置定义在 src/views/layout/config/layout.ts 中:

ts
import type { AppSettingPartial } from '@/store'
import { Component, defineAsyncComponent } from 'vue'

export interface LayoutFunctionItem {
  label: string
  appSettingKey: string
  default?: boolean
}

export interface LayoutMapItem {
  component: Component
  label: string
  appSetting: AppSettingPartial
  functions: LayoutFunctionItem[]
}

export type LayoutKey = 'mainLayout' | 'topMenuLayout'

export interface LayoutOption {
  label: string
  value: LayoutKey
}

export const layoutMap = new Map<LayoutKey, LayoutMapItem>([
  [
    'mainLayout',
    {
      component: defineAsyncComponent(
        () => import('../mainLayout/mainLayout.vue')
      ),
      label: '左侧菜单',
      appSetting: {
        menu: {
          mode: 'vertical'
        }
      },
      functions: [
        {
          label: '合并多模块菜单',
          appSettingKey: 'menu.mergeTopMenu'
        },
        {
          label: '折叠菜单',
          appSettingKey: 'menu.collapsed'
        },
        {
          label: 'logo',
          appSettingKey: 'showLogo'
        },
        {
          label: '面包屑',
          appSettingKey: 'breadcrumb.show'
        },
        {
          label: '面包屑图标',
          appSettingKey: 'breadcrumb.icon'
        },
        {
          label: '标签页',
          appSettingKey: 'routeHistory.show'
        },
        {
          label: '标签页按钮',
          appSettingKey: 'routeHistory.actions'
        },
        {
          label: '页脚',
          appSettingKey: 'footer.show'
        }
      ]
    }
  ],
  [
    'topMenuLayout',
    {
      component: defineAsyncComponent(
        () => import('../topMenuLayout/topMenuLayout.vue')
      ),
      label: '顶部菜单',
      appSetting: {
        menu: {
          mode: 'horizontal',
          collapsed: false
        }
      },
      functions: [
        {
          label: '合并多模块菜单',
          appSettingKey: 'menu.mergeTopMenu'
        },
        {
          label: 'logo',
          appSettingKey: 'showLogo'
        },
        {
          label: '面包屑',
          appSettingKey: 'breadcrumb.show'
        },
        {
          label: '面包屑图标',
          appSettingKey: 'breadcrumb.icon'
        },
        {
          label: '标签页',
          appSettingKey: 'routeHistory.show'
        },
        {
          label: '标签页按钮',
          appSettingKey: 'routeHistory.actions'
        },
        {
          label: '页脚',
          appSettingKey: 'footer.show'
        }
      ]
    }
  ]
])

export const layoutOptions: LayoutOption[] = [...layoutMap.entries()].map(
  ([key, item]) => ({
    label: item.label,
    value: key
  })
)

export const getLayout = (key: LayoutKey) => {
  return layoutMap.get(key)
}

左侧菜单布局(mainLayout)

默认布局,侧边栏菜单 + 内容区域。

顶部菜单布局(topMenuLayout)

菜单栏在顶部,适合菜单项较少的场景。

切换布局

布局通过应用设置(AppSetting)控制:

typescript
import { useStores } from '@/store'

const { app } = useStores()

// 切换到顶部菜单布局
app.setAppSetting({ layout: 'topMenuLayout' })

// 切换到左侧菜单布局
app.setAppSetting({ layout: 'mainLayout' })

AppSetting 配置

AppSetting 接口定义了布局相关的所有配置项:

typescript
interface AppSetting {
  colorPrimary: string // 主题色
  layout: LayoutKey // 布局模式
  showLogo: boolean // 显示 Logo
  menu: {
    mergeTopMenu: boolean // 合并多模块菜单
    collapsed: boolean // 折叠菜单
    mode: 'vertical' | 'horizontal' // 菜单模式
    backgroundColor: string // 菜单背景色
    textColor: string // 菜单文字颜色
    hoverBackgroundColor: string // 菜单悬停背景色
  }
  breadcrumb: {
    show: boolean // 显示面包屑
    icon: boolean // 面包屑图标
  }
  routeHistory: {
    show: boolean // 显示标签页
    actions: boolean // 标签页操作按钮
  }
  footer: {
    show: boolean // 显示页脚
  }
}

布局组件

布局系统包含以下子组件:

组件文件功能
Logocomponents/logo/logo.vue应用 Logo
Menucomponents/menu/menu.vue导航菜单
MenuCollapsedcomponents/menuCollapsed/menuCollapsed.vue折叠菜单
Breadcrumbcomponents/breadcrumb/breadcrumb.vue面包屑导航
RouteHistorycomponents/routeHistory/routeHistory.vue路由标签页
UserMenucomponents/userMenu/userMenu.vue用户菜单
ToggleDarkcomponents/toggleDark/toggleDark.vue暗色主题切换
AppSettingcomponents/appSetting/appSetting.vue应用设置面板
Footercomponents/footer/footer.vue页脚

基于 MIT 许可发布