我不明白为什么这意味着我不能将诺言的返回赋给布尔型。我尝试过的任何其他方式都告诉我,我无法为布尔类型分配一个空值。不知道从这里做什么
我正在尝试将checkJWT的输出分配给Promise的分辨率。从逻辑上讲,我可以做到这一点,但打字稿不喜欢它。
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { NotificationService } from '../index';
import { UserInterface } from '../../../models/index';
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor(
private notificationService: NotificationService,
private httpClient: HttpClient
) { }
checkJWT(role):boolean {
let canVisit: boolean = this.httpClient.get(window.location.protocol + '//' + window.location.hostname + ':3000/authenticate')
.toPromise()
.then(
(user: UserInterface) => {
return role === user.role;
}
) .catch((err) => { this.notificationService })
return canVisit;
}
答案 0 :(得分:2)
尝试一下
checkJWT(role):Promise<boolean>{
let canVisit: Promise<boolean>= <Promise<boolean>>this.httpClient.get(window.location.protocol + '//' + window.location.hostname + ':3000/authenticate')
.toPromise()
.then(
(user: UserInterface) => {
return role === user.role;
}
) .catch((err) => { this.notificationService })
return canVisit;
}
由于this.httpClient.get().toPromise()
返回一个Promise
对象