vben: 从接口返回菜单时要注意的两个环节
一,代码:
# , dependencies=[Depends(require_perms("admin:menu:list"))]
@router.get("/all", summary="全量菜单树(含按钮)")
async def menu_list(db: AsyncSession = Depends(get_db)) -> dict:
"""管理视图。与 GET /menu/routes 的区别:后者是给前端动态路由用的当前用户视图。"""
# 获取全部菜单
dict_one = {
'name':'Dashboard',
'path':'/dashboard',
'meta':{
'icon':'lucide:layout-dashboard',
'order':'',
'title':'page.dashboard.title',
},
'children':[
{
'name':'Analytics',
'path':'analytics',
'component':'/dashboard/analytics/index.vue',
'meta':{
'icon':'lucide:layout-dashboard',
'affixTab':'true',
'title':'page.dashboard.analytics',
},
},
{
'name':'Workspace',
'path':'workspace',
'component':'/dashboard/workspace/index.vue',
'meta':{
'icon':'carbon:workspace',
'title':'page.dashboard.workspace',
},
},
]
}
dict_two = {
'name':'Operations',
}
# 遍历菜单,生成带有父子结构的结果
list_res = [
dict_one,
# dict_two,
]
# 返回
return success(data = list_res)
二,要注意的地方:
1, vben项目本地代码用了locale实现,所以菜单项的title的值为: title: $t('page.dashboard.title'),
服务端不能返回:
title: $t('page.dashboard.title')
因为 $t() 是前端函数,服务端只应返回它的国际化 key 字符串:
{
"meta": {
"title": "page.dashboard.title"
}
}
Vben 菜单组件会自动执行翻译
2,component的路径:
Vben 在 access.ts 中通过:
const pageMap = import.meta.glob('../views/**/*.vue');
把字符串解析为真实组件。
例如页面文件:
apps/web-ele/src/views/operations/user/index.vue
对应的 component 是:
/operations/user/index
不要返回:
/views/operations/user/index.vue
浙公网安备 33010602011771号