簡體   English   中英

Angular2在canActivate Guard中探索已解析的數據嗎?

[英]Angular2 Exploring Resolved Data in canActivate Guard?

是否可以在canActivate防護中訪問路由的已解析數據(-Resolver)。 目前,我可以通過以下方式訪問組件中的已解析數據:

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

我該如何在canActivate Guard中進行管理? 這不起作用:

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);
        });
}

不可以 ,因為不能在解析之前調用canActivate方法,所以不能獲取數據

警衛處理

  1. canDeactivate

  2. canLoad

  3. canActivateChild

  4. canActivate

  5. 解決

嘿,我也遇到類似的問題。

我有一個解決方法,看看這是否對您有幫助,以及那些尋求類似問題的人有幫助。

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

route.root.children [i] .routeConfig.resolve下可用

我無法獲取數據,但在路由中傳遞解析可以達到目的。

resolve: {
    url: "<myurl>"
}

類似於: ActivatedRouteSnapshot不會顯示所有數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM