Redirect to a different component inside @CanActivate in Angular2

前端 未结 6 571
攒了一身酷
攒了一身酷 2020-12-03 10:03

Is there any way we can redirect to a different component from @CanActivate in Angular2 ?

6条回答
  •  死守一世寂寞
    2020-12-03 10:24

    This might help someone who is trying to wait on something before continuing.

    waitForBonusToLoad(): Observable<[boolean, string]> {
    
    const userClassBonusLoaded$ = this.store.select(fromRoot.getUserClasssBonusLoaded);
    const userClassSelected$ = this.store.select(fromRoot.getClassAttendancesSelectedId);
    
    return userClassBonusLoaded$.withLatestFrom(userClassSelected$)
      .do(([val, val2]) => {
        if (val === null && val2 !== null) {
          this.store.dispatch(new userClassBonus.LoadAction(val2));
        }
      })
      .filter(([val, val2]) => {
        if(val === null && val2 === null) return true;
        else return val;
      })
      .map(val => {
        return val;
      })
      .take(1);
    }
    
    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable {
    return this.waitForBonusToLoad().map(([val, val2]) =>
      {
        if(val === null && val2 === null){
          this.router.navigate(['/portal/user-bio']);
          return false;
        }
        else {
          return true;
        }
      }
    )
    }
    

提交回复
热议问题