[英]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:解决方案:
canActivate
canActivate
tap
, switchMap
operator.tap
、 switchMap
运算符。 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.您可以尝试使用
pipe
和map
运算符。
@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.