首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型'Promise<boolean | void>‘不能赋值给类型'boolean’

类型'Promise<boolean | void>‘不能赋值给类型'boolean’
EN

Stack Overflow用户
提问于 2019-02-11 13:52:44
回答 1查看 13.1K关注 0票数 1

我不明白为什么它说我不能将promise的返回值赋给布尔类型。我尝试过的任何其他方法都会告诉我,我不能将void赋给boolean类型。不知道从这里做什么

我正在尝试将checkJWT的输出赋值给promise的解析。从逻辑上讲,我可以这样做,但typescript不喜欢它。

代码语言:javascript
复制
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;
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-11 14:22:27

尝尝这个

代码语言:javascript
复制
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对象

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54624656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档