【问题标题】:Angular 2 routing issue child and parentAngular 2路由问题子和父
【发布时间】:2017-01-07 11:12:23
【问题描述】:

我在 Stack Overflow 上发现了许多路由问题,但没有回答我的问题。用户登录后,用户被路由到具有子路由的父级:

{
    path: '',
    component: LoginComponent,
},
{
    path: 'parent',
    component: ParentComponent,
    children: [
        {
            // without this routing does not work
            path: '',
            redirectTo: 'parent'
        },
        { 
            path: 'child', 
            component: ChildComponent,
        },
    ]
},
//... more routes

我的问题是没有children中提到的以下内容:

            path: '',
            redirectTo: 'parent'

用户根本不会从登录页面路由到父路由。但这也与我的网址混淆,而不仅仅是

localhost:3000/parent 我得到localhost:3000/parent/parent。那仍然显示正确的组件。但是当用户导航到别处时,出现错误:

无法匹配任何路线:'something/something'

如何解决这个问题,谢谢?

【问题讨论】:

    标签: angular


    【解决方案1】:

    你的 url 的问题可能是当你导航到你的父组件时引起的,如果路径是'',在你的情况下意味着localhost:3000/parent

    然后你又redirectToparent,所以导致localhost:3000/parent/parent

    尝试删除redirectTo: 'parent',看看会发生什么!

    【讨论】:

    • 但是我写了 route does not route if I leave it away.
    • 你是不是把path: ''留在了孩子里面,然后把redirectTo: 'parent'去掉?
    • 你也可以重定向到child
    猜你喜欢
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 2017-11-20
    • 2016-09-23
    • 1970-01-01
    相关资源
    最近更新 更多