简体   繁体   English

Angular RouteGuard“声明类型既不是‘void’也不是‘any’的函数必须返回一个值”

[英]Angular RouteGuard "A function whose declared type is neither 'void' nor 'any' must return a value"

I have a route guard that checks if a user has access to a resource, before proceeding.我有一个路由守卫,在继续之前检查用户是否有权访问资源。 After the subscription, I check if the user has access, and if not, redirect using parseUrl or return true .订阅后,我检查用户是否具有访问权限,如果没有,则使用parseUrl重定向或返回true

@Injectable({ providedIn: 'root' })
export class PortfolioGuard implements CanActivate {
  constructor(private resourceAccessService: ResourceAccessService, private router: Router) { }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean | UrlTree {
    const id = +route.paramMap.get('id');
    // return this.resourceAccessService.checkUserAccessToPortfolio(id);
    this.resourceAccessService.checkUserAccessToPortfolio(id).subscribe((hasAccess) => {
      if (!hasAccess) {
        console.log('No access');
        return this.router.parseUrl('/');
      } else {
        console.log('access');
        return true;
      }
    });
  }
}

This works if I only return the check, but I also want it to redirect based on that check.如果我只返回支票,这有效,但我也希望它根据该支票重定向。

@Injectable({ providedIn: 'root' })
export class PortfolioGuard implements CanActivate {
  constructor(private resourceAccessService: ResourceAccessService, private router: Router) { }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    const id = +route.paramMap.get('id');
    return this.resourceAccessService.checkUserAccessToPortfolio(id);
  }
}

What am I missing in the first block of code, should I be resolving the subscription from the service...我在第一个代码块中遗漏了什么,我应该从服务中解析订阅...

checkUserAccessToPortfolio(id: number): Observable<boolean>  {
    const url = this.baseUrl + this.userResourceAccessUrl + '/' + + id + '/portfolio';
    return this.http.get<boolean>(url);
  }

I would try to return the Observable in the function:我会尝试在函数中返回 Observable:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean | UrlTree {
  const id = +route.paramMap.get('id');
    
  return this.resourceAccessService.checkUserAccessToPortfolio(id).pipe(
    map(hasAccess => {
      if (!hasAccess) {
        console.log('No access');
        this.router.parseUrl('/');
        return false
      } else {
        console.log('access');
        return true;
      }
    })
  )
}

The is issue is very obviously.问题很明显。 You did not return anything in your function.您没有在函数中返回任何内容。

Solution:解决方案:

  1. add return to your function canActivate将返回添加到您的函数canActivate
  2. remove subscribe and move all logic to map operator.删除订阅并将所有逻辑移至映射运算符。 Or you can use tap , switchMap operator.或者您可以使用tapswitchMap运算符。 it depends on your context.这取决于你的上下文。

Instead of doing this:而不是这样做:

@Injectable({ providedIn: 'root' })
export class PortfolioGuard implements CanActivate {
  constructor(private resourceAccessService: ResourceAccessService, private router: Router) { }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean | UrlTree {
    const id = +route.paramMap.get('id');
    // return this.resourceAccessService.checkUserAccessToPortfolio(id);
    this.resourceAccessService.checkUserAccessToPortfolio(id).subscribe((hasAccess) => {
      if (!hasAccess) {
        console.log('No access');
        return this.router.parseUrl('/');
      } else {
        console.log('access');
        return true;
      }
    });
  }
}

You can try using pipe and map operator.您可以尝试使用pipemap运算符。

@Injectable({ providedIn: 'root' })
export class PortfolioGuard implements CanActivate {
  constructor(private resourceAccessService: ResourceAccessService, private router: Router) { }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean | UrlTree {
    const id = +route.paramMap.get('id');
    return this.resourceAccessService.checkUserAccessToPortfolio(id).pipe( map( hasAccess => {
      if(!hasAccess) {
        console.log('No access');
        this.router.parseUrl('/');
        return false
      } else {
        console.log('access');
        return true;
      }
    });
  }
}

暂无
暂无

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

相关问题 声明类型既不是“void”也不是“any”的函数必须返回一个值 Angular - A function whose declared type is neither 'void' nor 'any' must return a value Angular angular 中的 TypeScript 错误:声明类型既不是“void”也不是“any”的 function 必须返回一个值 - TypeScript error in angular: A function whose declared type is neither 'void' nor 'any' must return a value 打字稿 - 声明类型既不是“void”也不是“any”的函数必须返回一个值 - Typescript - A function whose declared type is neither 'void' nor 'any' must return a value 错误TS2355:声明的类型既不是“ void”也不是“ any”的函数必须返回一个值 - Error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value 声明类型既不是“void”也不是“any”的函数必须返回一个值 - A function whose declared type is neither 'void' nor 'any' must return a value Ts错误:声明的类型既不是&#39;void&#39;也不是&#39;any&#39;的函数必须返回一个值。 当return语句在订阅方法中时 - Ts Error : A function whose declared type is neither 'void' nor 'any' must return a value. when return statements are inside a subscribe method 订阅登录组件时,login方法抛出错误。声明类型既不是&#39;void&#39;也不是&#39;any&#39;的函数必须返回一个值 - login method is throwing error while subscribing in login component.A function whose declared type is neither 'void' nor 'any' must return a value DEXIE-一个其decl ared类型既不是&#39;void&#39;也不是&#39;any&#39;的函数必须返回一个值 - DEXIE - A function whose decl ared type is neither 'void' nor 'any' must return a value Angular,类型“string”不可分配给类型“(value: any) => void” - Angular, Type 'string' is not assignable to type '(value: any) => void' Angular2 Observable - 函数必须返回值 - Angular2 Observable - Function must return value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM