简体   繁体   English

Angular2在canActivate Guard中探索已解析的数据吗?

[英]Angular2 Exploring Resolved Data in canActivate Guard?

Is it possible to access resolved data of a route (-Resolver) inside a canActivate guard. 是否可以在canActivate防护中访问路由的已解析数据(-Resolver)。 Currently I can access the resolved data in the component by 目前,我可以通过以下方式访问组件中的已解析数据:

ngOnInit() {
    this.route.data
        .subscribe((data: { example: Array<Object> }) => {
            this.example = data.example;
            console.log('example resolver', this.example);
        });
}

How could I manage that in the canActivate guard? 我该如何在canActivate Guard中进行管理? This is not working: 这不起作用:

constructor(private route: ActivatedRoute) {}

canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot,
): boolean {

    this.route.data
        .subscribe((data: { example: Array<Object> }) => {
            this.example = data.example;
            console.log('example resolver', this.example);
        });
}

No , you can't because canActivate Method is called before resolve, so you can't get the data 不可以 ,因为不能在解析之前调用canActivate方法,所以不能获取数据

Guard Processing : 警卫处理

  1. canDeactivate canDeactivate

  2. canLoad canLoad

  3. canActivateChild canActivateChild

  4. canActivate canActivate

  5. resolve 解决

Hey I was also stuck with similar problem. 嘿,我也遇到类似的问题。

I have a workaround see if that helps your purpose and those who come looking for similar problem. 我有一个解决方法,看看这是否对您有帮助,以及那些寻求类似问题的人有帮助。

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    window.location.href = route.root.children[0].routeConfig.resolve.url;
    return true;
}

it is available under route.root.children[i].routeConfig.resolve route.root.children [i] .routeConfig.resolve下可用

I couldn't get data but passing the resolve in route served the purpose. 我无法获取数据,但在路由中传递解析可以达到目的。

resolve: {
    url: "<myurl>"
}

Similar to : ActivatedRouteSnapshot not showing all data 类似于: ActivatedRouteSnapshot不会显示所有数据

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

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