订阅不可分配给类型路由
Subscription is not assigneable to type route
我正在尝试设置 canActivate 路由,但出现语法错误(订阅不可分配给类型路由):
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const pin = route.paramMap.get('pin');
let canActivate;
if (pin) {
return this.partService.getCached(pin).pipe(
take(1),
concatMap(p => {
if(this.appService.isUserAuthorized(Authorization.canAccessAll_View, p))
{
return of(true);
}
return this.partService.getEnrolled(pin,'WW')
})).subscribe(parts => {
if(parts)
{
parts.forEach(part => {
if (part.agencyCode == this.appService.user.agencyCode) {
if (this.appService.isUserAuthorizedToView(part)) {
canActivate = true;
} else {
this.routeToUnauthorized(state.url);
canActivate = false;
}
}
});
}
return canActivate;
});
}
}
这里的语法有什么问题?
任何帮助将不胜感激。
尝试映射而不是订阅。就像 cghislai 提到的,你必须 return 布尔值、Promise 或 Observable;
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const pin = route.paramMap.get('pin');
let canActivate = false; // initialize with a false value
if (pin) {
return this.partService.getCached(pin).pipe(
take(1),
concatMap(p => {
if(this.appService.isUserAuthorized(Authorization.canAccessAll_View, p))
{
return of(true);
}
return this.partService.getEnrolled(pin,'WW');
}),
map(parts => {
if(parts)
{
parts.forEach(part => {
if (part.agencyCode == this.appService.user.agencyCode) {
if (this.appService.isUserAuthorizedToView(part)) {
canActivate = true;
} else {
this.routeToUnauthorized(state.url);
canActivate = false;
}
}
});
}
return canActivate;
}));
}
我正在尝试设置 canActivate 路由,但出现语法错误(订阅不可分配给类型路由):
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const pin = route.paramMap.get('pin');
let canActivate;
if (pin) {
return this.partService.getCached(pin).pipe(
take(1),
concatMap(p => {
if(this.appService.isUserAuthorized(Authorization.canAccessAll_View, p))
{
return of(true);
}
return this.partService.getEnrolled(pin,'WW')
})).subscribe(parts => {
if(parts)
{
parts.forEach(part => {
if (part.agencyCode == this.appService.user.agencyCode) {
if (this.appService.isUserAuthorizedToView(part)) {
canActivate = true;
} else {
this.routeToUnauthorized(state.url);
canActivate = false;
}
}
});
}
return canActivate;
});
}
}
这里的语法有什么问题? 任何帮助将不胜感激。
尝试映射而不是订阅。就像 cghislai 提到的,你必须 return 布尔值、Promise 或 Observable;
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const pin = route.paramMap.get('pin');
let canActivate = false; // initialize with a false value
if (pin) {
return this.partService.getCached(pin).pipe(
take(1),
concatMap(p => {
if(this.appService.isUserAuthorized(Authorization.canAccessAll_View, p))
{
return of(true);
}
return this.partService.getEnrolled(pin,'WW');
}),
map(parts => {
if(parts)
{
parts.forEach(part => {
if (part.agencyCode == this.appService.user.agencyCode) {
if (this.appService.isUserAuthorizedToView(part)) {
canActivate = true;
} else {
this.routeToUnauthorized(state.url);
canActivate = false;
}
}
});
}
return canActivate;
}));
}