| | |
| | | |
| | | /** 查找对应 `path` 的路由信息 */ |
| | | function findRouteByPath(path: string, routes: RouteRecordRaw[]) { |
| | | let res = routes.find((item: { path: string }) => item.path == path); |
| | | let res = routes?.find((item: { path: string }) => item.path == path); |
| | | if (res) { |
| | | return isProxy(res) ? toRaw(res) : res; |
| | | } else { |
| | | for (let i = 0; i < routes.length; i++) { |
| | | for (let i = 0; i < routes?.length; i++) { |
| | | if ( |
| | | routes[i].children instanceof Array && |
| | | routes[i].children.length > 0 |
| | | routes[i]?.children instanceof Array && |
| | | routes[i]?.children.length > 0 |
| | | ) { |
| | | res = findRouteByPath(path, routes[i].children); |
| | | if (res) { |
| | |
| | | (v: RouteRecordRaw) => { |
| | | // 防止重复添加路由 |
| | | if ( |
| | | router.options.routes[0].children.findIndex( |
| | | router.options.routes[0].children?.findIndex( |
| | | value => value.path === v.path |
| | | ) !== -1 |
| | | ) { |
| | | return; |
| | | } else { |
| | | // 切记将路由push到routes后还需要使用addRoute,这样路由才能正常跳转 |
| | | router.options.routes[0].children.push(v); |
| | | router.options.routes[0].children?.push(v); |
| | | // 最终路由进行升序 |
| | | ascending(router.options.routes[0].children); |
| | | if (!router.hasRoute(v?.name)) router.addRoute(v); |