我不明白为什么它说我不能分配承诺的到布尔类型的回报。我曾尝试任何其他方式告诉我,我不能分配的空隙布尔类型。不知道该怎么在这里做
我想checkJWT的输出分配给承诺的分辨率。这在逻辑上是有道理的,我认为我能做到这一点,但打字稿不喜欢它。
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;
}
尝试这个
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
对象