文档地址、 演示地址、 源码地址
复制代码export interface AdminPlugin {
id: string // 插件唯一标识
name: string // 插件名称
version: string // 插件版本
enabled?: boolean // 默认是否启用
order?: number // 注册顺序
dependsOn?: string[] // 依赖的插件 id
routes?: AdminRouteRecordRaw[] // 插件提供的路由
menus?: MenuConfig[] // 插件提供的菜单
locales?: {
'zh-CN'?: Record<string, any>
en?: Record<string, any>
} // 插件自己的语言包
install?: (app: App, context: PluginContext) => void | Promise<void> // 初始化钩子
}
我比较在意的一点是,插件要把自己的边界说清楚,而不是嘴上说自己是“独立模块”,代码里还在不停伸手改宿主,所以后面的插件架构要设计好,大概做了以下功能点





id、name、version、enabled、order、dependsOn、routes、menus、locales、install 这些字段先收口order 控制接入顺序enabled 配置插件初始状态id、路由 path / name 和菜单 pathlocales 能注入到宿主install 钩子,方便插件做自己的初始化localStorage 复制代码const reportPlugin: AdminPlugin = {
id: 'plugin-report',
name: '报表插件',
version: '1.0.0',
order: 20,
dependsOn: ['plugin-shop'],
routes: [
{
path: '/report/list',
name: 'ReportList',
component: () => import('./views/ReportList.vue'),
meta: {
title: 'report.list',
layout: 'default',
permissions: ['report.read']
}
},
{
path: '/report/detail/:id',
name: 'ReportDetail',
component: () => import('./views/ReportDetail.vue'),
meta: {
title: 'report.detail',
layout: 'default',
permissions: ['report.read'],
activeMenu: '/report/list',
noCache: true,
hidden: true
}
}
],
menus: [
{
path: '/report',
title: 'report.title',
icon: 'chart',
children: [
{
path: '/report/list',
title: 'report.list'
}
]
}
]
}
这段代码最关键的地方,不是“能跑起来”,而是它把自己该交给宿主的信息一次性带齐了。宿主看到这个插件,基本就知道要把什么东西接进去。
做到这里之后,我最直接的感受就是,新增一个业务模块时,接入方式终于稳定下来了。对我来说,这就够了。至少这个中后台后面继续长的时候,不会再回到什么都往宿主里塞的状态。