视图模块和路径前缀
本文导航

视图模块和路径前缀
动态路由不会在菜单数据里带上 Vue 组件。后端只给路径和组件名字符串,前端用视图模块表把它对上懒加载函数。
glob 的键会被改写
子系统这样提供视图:
viewModules: import.meta.glob("./views/**/*.{vue,tsx}"),
viewModulesPathPrefix: "/order"
文件 ./views/list/index.vue 在 registry 里变成 /order/list/index.vue。改写规则是把键开头的 ./views 换成前缀,见 registerSubsystems。
前缀的回退顺序:
viewModulesPathPrefix- 第一条静态路由的
path /${name}
后端菜单要写成什么
addAsyncRoutes 用后端的 component,没有就用 path,在所有视图键里做 includes 匹配,取第一个命中的加载函数。
所以菜单的 component 应该能被上面的键包含,例如:
| 文件 | 改写后的键 | 后端 component |
|---|---|---|
./views/list/index.vue |
/order/list/index.vue |
/order/list/index |
/order/list/index 能被 /order/list/index.vue 包含,匹配成功。写成 /orders/list/index 就对不上,页面是空白,路由却已经加上了。
框架自己的页面来自 import.meta.glob("/src/views/**/*.{vue,tsx}"),和子系统的表合并后再匹配。两个包如果写出能互相 includes 的路径,会命中先注册的那个。前缀要带上子系统自己的目录,不要都用 /index。
页面组件还要:
<script setup lang="ts">
defineOptions({ name: "OrderList" });
</script>
name 和菜单的 Name、路由 name 一致。多标签和 keep-alive 用的是这个名字。
评论
加载评论中…