角度2重定向到守卫的儿童路线

时间:2017-05-13 04:36:59

标签: angular angular2-routing

我定义了以下路线:

{
    path: "dashboard/:id",
    component: dashboard.DashboardComponent,
    canActivate: [guard.LoggedInGuard],
    children: [
        {
            path: "dock/:dockid",
            children: [
                {
                    path: "",
                    component: dock.DockComponent,
                    canActivate: [guard.DockExistsGuard],
                }
            ]
        },
        {
            path: "dock-request",
            component: dashboard.DockRequestComponent,
            pathMatch: "full"
        }
    ...

内部DockExistsGuard如果未满足X条件,我正尝试重定向到dock-request。我目前正在使用它,但我觉得我的实现是错误的 - (感觉很讨厌)。

this.router.navigate(['/dashboard/' + id + '/dock-request']);

有没有更好的方法来实现此重定向?

我在这里:

  

http://localhost:4200/dashboard/136364285/home

试图到达这里:

  

http://localhost:4200/dashboard/136364285/dock/71672341

但需要从.guard重定向到此处:

  

http://localhost:4200/dashboard/136364285/dock-request

1 个答案:

答案 0 :(得分:0)

所以,虽然我还没有发现有必要这样做,但我同意这是一个完全合理的期望。查看路由器文档,似乎您可以通过指定子树(使用with open('dictionary.txt') as f: all_words = [word for line in f for word in line.split()] word_count = len(all_words) )进行导航,如下所示:

relativeTo

您似乎可以按照以下方式使用:

// navigate to /team/33/user/22
router.createUrlTree(['../22'], {relativeTo: route});

在您的具体情况下,您应该能够做类似的事情:

router.navigate(['../22'], {relativeTo: route});

希望有所帮助!