角嵌套子级路由不路由

时间:2018-07-26 15:01:24

标签: angular angular-routing

我正在尝试创建一些嵌套的子级路由。

第一个父级路由是防止用户未登录时访问应用程序上的任何内容。这就是<dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> 的作用。

然后,我有一个管理员组件,仅当用户是管理员时才能访问。它的子项也需要受到AuthGuardService的保护。

AdminGuardService路由外,所有这些路由似乎都有效。当我去users时,什么都没有发生。它不会导航,也不会给我错误。它只是位于您所在的页面上。我希望它能贯穿admin/users,然后路由到adminguard页面。

当我走users路线并将其移到儿童区之外时,它会起作用。在下面的代码中,我的评论指出了该位置。

我在做什么错?我该如何使用它?

admin/users

这也不起作用。

const routes: Routes = [
    {
        path: "",
        component: BaseComponent,
        canActivate: [AuthGuardService],
        children: [
            {path: "", redirectTo: "/boards", pathMatch: "full"},
            {path: "boards", component: BoardsComponent},
            {
                path: "admin",
                component: AdminComponent,
                canActivate: [AdminGuardService],
                children: [
                    {
                        path: "",
                        children: [
                            {path: "users", component: AdminUsersComponent}
                        ]
                    }
                ]
            },
            // {path: "admin/users", component: AdminUsersComponent}
        ]
    },
    {
        path: "login",
        component: LoginComponent
    }
];

这有效,但不是我想要的...

const routes: Routes = [
    {
        path: "admin",
        component: AdminComponent,
        children: [
            { path: "users", component: AdminUsersComponent },
        ]
    }
]

1 个答案:

答案 0 :(得分:1)

这确实是很奇怪的行为,所以我尝试重新创建问题并尝试了这种结构,它确实起作用了。除非您的防护装置内部有任何破损的地方,否则此结构应该起作用:

   {
    path: "admin",
    canActivate: [AdminGuardService],
    children: [
      {
        path: "",
        component: AdminComponent
      },
      {
        path: "users",
        component: AdminUsersComponent
      }
    ]
  },

在我的尝试中,没有Guard,其结构为:

  

/
/ admin
/ admin / test