Vue.js 如何按路由名称转到子路由?
我知道我们可以使用Vue.js 如何按路由名称转到子路由?,vue.js,vue-router,Vue.js,Vue Router,我知道我们可以使用$router.push({name:'route name'})按名称进入路由 我想知道的是,如何使用子路由名称。 这是我的路线结构: 导出默认值[ { 路径:“/”, 姓名:'家', 组件:()=>导入('@/views/Home.vue'), 儿童:[ { 名字:'家,关于', 路径:'/about', 组件:导入(“@/views/Home/About.vue”) } ] } ] 但是我的控制台显示,$router.push({name:'home.about')的名
$router.push({name:'route name'})
按名称进入路由
我想知道的是,如何使用子路由名称
。
这是我的路线结构:
导出默认值[
{
路径:“/”,
姓名:'家',
组件:()=>导入('@/views/Home.vue'),
儿童:[
{
名字:'家,关于',
路径:'/about',
组件:导入(“@/views/Home/About.vue”)
}
]
}
]
但是我的控制台显示,$router.push({name:'home.about')的名为“home.about”的[vue router]路由不存在
我错过了什么
Obs:我们的想法是不要使用硬的路由路径给孩子路由,因为你有一个打字错误
它应该是children
,而不是children
export default [
{
path: '/',
name: 'home',
component: () => import('@/views/Home.vue'),
children: [
{
name: 'home.about',
path: '/about',
component: import('@/views/Home/About.vue')
}
]
}
]
现在,如果你想导航到fooChild1
,那么就使用$router.push({name:'fooChild1'})或者如果你想导航到fooChild2
,那么就使用$router.push({name:'fooChild2'})将精确添加到特定的路由。我把exact
正确地放在哪里?你需要把它放进去:这里没有
。我首先说的是$router
,你能检查一下名字应该是“children”而不是“child”吗。其次,请尝试从儿童路线中删除“/”,如“about”。
const router = new VueRouter({
routes: [{
path: '/foo',
name: 'foo',
component: Foo,
children: [
{
path: 'fooChild1',
name: 'fooChild1',
component: FooChildComponent
},
{
path: 'fooChild2',
name: 'fooChild2',
component: FooChildComponent
}
]
}, {
path: '/bar',
component: Bar
}]
})