vue获取当前路由的父路由的所有子路由
时间: 2024-03-10 19:17:54 浏览: 22
可以通过$route.matched来获取当前路由的所有匹配记录,然后再从中筛选出父路由的所有子路由。
具体实现方法如下:
1. 获取当前路由的所有匹配记录
```javascript
const matched = this.$route.matched;
```
2. 筛选出父路由的所有子路由
```javascript
const parentRoute = matched.find(record => record.path === '/parent');
const childrenRoutes = parentRoute ? parentRoute.children : [];
```
其中,/parent为父路由的路径,可以根据实际情况修改。
完整代码如下:
```javascript
const matched = this.$route.matched;
const parentRoute = matched.find(record => record.path === '/parent');
const childrenRoutes = parentRoute ? parentRoute.children : [];
```
需要注意的是,childrenRoutes并不包含父路由本身,只包含其子路由。如果需要同时获取父路由,可以将其添加到数组中,如下:
```javascript
const matched = this.$route.matched;
const parentRoute = matched.find(record => record.path === '/parent');
const childrenRoutes = parentRoute ? [parentRoute, ...parentRoute.children] : [];
```