簡體   English   中英

在angular2中,我使用的是canActivate,但是當它返回false時,它仍然會轉到該路線

[英]In angular2 I'm using canActivate but when it returns false it still goes to the route

在angular2中,我使用的是canActivate,但是當它返回false時,它仍然會轉到該路由。

這是我的代碼如下:

 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        this.isAuthRequired = route.data.isAuthRequired;
        this.isCompanyView = route.data.isCompanyView;

        return this.init();  
  }

此代碼根據路由數據(例如isAuthRequired等)返回true或false。

這仍然意味着要去那條路線嗎? 因為它確實可以,並且只顯示藍屏,沒有控制台錯誤,這很好,但是我希望它重定向到主頁,或者至少保留在上一頁。

此功能是如何工作的,您有什么建議嗎?

確保您從init()方法返回booleanpromiseobservar 從init()返回true將導航到選定的路由。 返回false將阻止從當前屏幕進行頁面導航。

像下面這樣:

init(): boolean{
    /* your application logic */

    return true;
}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    this.isAuthRequired = route.data.isAuthRequired;
    this.isCompanyView = route.data.isCompanyView;

    return this.init();  
}

希望對您有所幫助。

暫無
暫無

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

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