gpt4 book ai didi

angular - 错误 TS2355 : A function whose declared type is neither 'void' nor 'any' must return a value

转载 作者:行者123 更新时间:2023-12-04 01:31:26 25 4
gpt4 key购买 nike

我在 Angular 5 中使用身份验证保护来检查用户是否应该能够导航到特定页面。我收到与返回 observable 相关的错误:

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

这是我的 authguard.ts

从'@angular/core'导入{可注入(inject)};
从'@angular/router'导入{路由器,CanActivate,ActivatedRoute,ActivatedRouteSnapshot,RouterStateSnapshot};
从'../../service/application-verification.service'导入{ApplicationVerificationService};

@Injectable() export class AppStatusGuard implements CanActivate {

applicationId: number; personId: number;

constructor(
public router: Router,
public readonly activeRoute: ActivatedRoute,
public readonly applicationVerificationService: ApplicationVerificationService) { }

loginRoute: boolean; memberRoute: boolean;


canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {

this.applicationId = Number(route.params["applicationId"]);
this.personId = Number(route.params["personId"]);

// Make sure they are allowed on this page, if not re-direct
const url = this.applicationVerificationService.getRouteForFellowshipApplicant(this.personId, this.applicationId)
.subscribe((res: string) => {
console.log(url);

if (res !== `application-status/${this.personId}/${this.applicationId}`) {
console.log("failure");
// this.router.navigateByUrl(url);
return false;
} else {
console.log("success");
//this.router.navigateByUrl(url);
return true;
}
});
};
};

这是我的验证服务
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class ApplicationVerificationService {
constructor(
private readonly httpClient: HttpClient,
private readonly router: Router) { }

getRouteForFellowshipApplicant(personId, applicationId): Observable<string> {
this.httpClient.get('api/application-verification/' + personId).subscribe((responseObject: IFellowshipApplicationStatus) => {

return Observable.create(observer => {

if (responseObject.preApplicationVerificationStatus === 'GoToTrackerPage') {
observer.next('application-status/' + personId + '/' + applicationId);
}
if (responseObject.preApplicationVerificationStatus === 'GoToApplicationFormPage') {
observer.next('application-form/' + personId + '/' + applicationId);
}
if (responseObject.preApplicationVerificationStatus === 'GoToContactIceForInfoPage') {
observer.next('contact-us/' + personId + '/' + applicationId);
}
if (responseObject.preApplicationVerificationStatus === 'GenericError') {
observer.next('application-error/' + personId);
}
observer.complete();
});
});
return null;
}
}

我是 rxjs 的新手,我认为我在返回 observable 的方式上犯了一个错误,但我有点挣扎。任何帮助,将不胜感激。

最佳答案

这将解决您的问题。

您的问题是您没有返回任何内容,并且您正在订阅您的 HTTP 调用。相反,返回 Observable 本身,并使用 pipe(map(...))在您的 HTTP 调用之后执行操作,而不会中断诸如 subscribe 之类的操作链做。

return this.applicationVerificationService.getRouteForFellowshipApplicant(this.personId, this.applicationId)
.pipe(map(res => {
console.log(url);

if (res !== `application-status/${this.personId}/${this.applicationId}`) {
console.log("failure");
// this.router.navigateByUrl(url);
return false;
} else {
console.log("success");
//this.router.navigateByUrl(url);
return true;
}
}));
};

关于angular - 错误 TS2355 : A function whose declared type is neither 'void' nor 'any' must return a value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51595481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com