Angular 7-如何检查用户是否直接导航到页面

时间:2019-10-01 08:25:29

标签: angular routing angular-routing

我有一个具有下一个路由结构的应用程序:

{
    path: "home",
    component: HomeComponent,
    children: [
        {
            path: "list",
            component: ListViewComponent
        },
        {
            path: "dashboard",
            component: DashboardComponent
        },
        {
            path: '',
            pathMatch: 'full',
            redirectTo: 'dashboard'
        },
        {
            path: "**",
            redirectTo: 'dashboard'
        }
    ]
}

如何检查用户是否通过完整路径(例如home/listredirectTo进入页面?

2 个答案:

答案 0 :(得分:2)

您可以通过添加以下内容来检查网址:

path: 'team/:id',
component: TeamComponent,
canActivate: ['canActivateTeam']

这里['canActivateTeam']可以是服务中的函数。

答案 1 :(得分:0)

您可以在导航到url时使用state并将其传递给用户定义的值。

根据角度文档:

  

开发人员定义的状态,可以传递给任何导航。在执行导航时,通过从router.getCurrentNavigation()返回的Navigation.extras对象访问该值。

有关更多信息,请参见:https://angular.io/api/router/NavigationExtras