简体   繁体   English

继承的路由数据在 CanActivate 守卫中不起作用

[英]Inherited routing data does not work in CanActivate guard

Routing settings: paramsInheritanceStrategy: 'always'路由设置: paramsInheritanceStrategy: 'always'

Works in 99%.工作在 99%。 Params and data from higher components are passed to lower.来自较高组件的参数和数据被传递到较低组件。 Both are accessible in components in activated route params and data.两者都可以在激活的路由参数和数据的组件中访问。 The problem occurs in CanActivate.问题出现在 CanActivate 中。 Let's say we have such routing:假设我们有这样的路由:

path: '/a',
data: {
  param1: true
},
resolve: {subject: SubjectResolver},
children: [
  {
    path: ':someParammeeero',
    component: B,
    canActivate: [BGuard]
  },
  {
    path: '',
    component: A
  }
]

Component A has a button that redirects to component B. When am in component AI have access to the route.data.subject but when I try to go to component B, inside the BGuard activated route there is no subject inside data but there is param1 so... it's like the inheritance works but not in resolve case.组件 A 有一个重定向到组件 B 的按钮。当我在组件 AI 中时可以访问 route.data.subject 但是当我尝试转到组件 B 时,在 BGuard 激活的路由中,数据中没有主题,但有 param1所以......就像继承工作但不是在解决情况下。 Inside guard I have to access route.parent.data.subject.内部守卫我必须访问 route.parent.data.subject。

When am on component B and refresh the page, all works correctly.当我在组件 B 上并刷新页面时,一切正常。

I have two different behaviors inside the guard.我在警卫内部有两种不同的行为。

  1. When I navigate to the B component I have to access route.parent.data.subject.当我导航到 B 组件时,我必须访问 route.parent.data.subject。
  2. When being at the B component page I and refresh the page I have to access route.data.subject当我在 B 组件页面并刷新页面时,我必须访问 route.data.subject

Is it a bug or a feture?这是一个错误还是一个特征? Maybe my routing structure is incorrent?也许我的路由结构不正确?

The problem was the way I was navigating from component A to component B问题是我从组件 A 导航到组件 B 的方式

this.router.navigate(..., {relatedTo: this.activatedRoute})

'relatedTo' in some way removed inheritance from /a to /a:someParammeeero 'relatedTo' 以某种方式将继承从 /a 移除到 /a:someParammeeero

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM