我不明白为什么它说我不能将promise的返回值赋给布尔类型。我尝试过的任何其他方法都会告诉我,我不能将void赋给boolean类型。不知道从这里做什么
我正在尝试将checkJWT的输出赋值给promise的解析。从逻辑上讲,我可以这样做,但typescript不喜欢它。
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;
}发布于 2019-02-11 14:22:27
尝尝这个
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对象
https://stackoverflow.com/questions/54624656
复制相似问题