【问题标题】:Angular 7 - how to check if the user directly navigate into the pageAngular 7 - 如何检查用户是否直接导航到页面
【发布时间】:2019-10-01 08:25:29
【问题描述】:

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

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

如何检查用户是否通过完整路径导航到页面,例如home/listredirectTo

【问题讨论】:

    标签: angular routing angular-routing


    【解决方案1】:

    您可以通过添加来查看网址:

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

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

    【讨论】:

    • 我认为应该是一个更优雅的方法来检查这个。
    • 您实际上在寻找什么类型的方法?
    • 我不想创建一个单独的服务+警卫来检查这个。或许我们可以通过ActivatedRouteRouter 来实现。
    【解决方案2】:

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

    根据角度文档:

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

    更多信息请参考:https://angular.io/api/router/NavigationExtras

    【讨论】:

    • 与手动导航有关,在我的情况下,我需要通过路由配置来捕捉它。
    • @TarasKovalenko 您应该能够类似地使用数据。 stackoverflow.com/questions/36835123/… 可能会帮助你
    猜你喜欢
    • 1970-01-01
    • 2020-06-05
    • 2021-02-01
    • 2021-02-16
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多