这能解决您的问题吗?这里有一个示例代码笔 https://codepen.io/anon/pen/MLrpmR。您需要了解和设置的所有内容 - 路线名称(甚至更多 - 您可以改为使用meta
甚至path
特性)。您甚至可以创建递归函数,它将搜索每条路线,包括每条路线children
array.
这是一个简单的函数,用于搜索知道父路径名的子组件:
function recursiveChildrenSearch(routes, name) {
for (let route of routes) {
if (route.name === name)
return route.children;
else if (route.children.length > 0)
return recursiveChildrenSearch(route.children, name);
}
}
可以这样调用:
recursiveChildrenSearch(router.options.routes, "one")
调用示例路由对象:
routes: [
{
path: '/hello',
name: "main",
component: A,
children: [
{name: 'one', path: '1', component: B,
children: [
{name: 'subone', path: '1.1', component: C}
]},
{name: 'two', path: '2', component: C},
],
},
]
将返回:
[Object { name: "subone", path: "1.1", component: {…} }]