typescript http客户端无法解析布尔响应

时间:2017-12-14 14:26:52

标签: angular typescript angular-services

HttpClient未解析布尔值

服务

 public isUSCustomer(): Observable<Boolean> {

  return this.httpClient
        .get<Boolean>(myurl);

}

组件

private isUScustomer: Boolean; 
this.myservice.isUSCustomer()
              .subscribe(x => {
                   this.isUScustomer = x;
                   console.log(this.isUScustomer); //Its undefined 
                   console.log(x); //it logs true.
});

输出

console.log(this.isUScustomer); undefined
console.log(x); //it logs true.
console.log(typeof x); boolean

我尝试使用界面Booleanboolean这样。

return this.httpClient
        .get<boolean>(myurl);

and

return this.httpClient
        .get<Boolean>(myurl);

Api结果

true

我读了这个Typescript boolean conversion,但那是2013年。

版本信息

打字稿:2.4.2
Angular:5.0.2

4 个答案:

答案 0 :(得分:1)

我几天前看到同样的错误我偶然发现了一个叫做eval函数的东西,这就是你怎么可能尝试这个运行,

private isUScustomer: Boolean; 
this.myservice.isUSCustomer()
              .subscribe(x => {
                   this.isUScustomer = eval(x);
});

修改1

TS查找http调用的返回类型和数据类型,您也许可以尝试创建一个接口并给它一个去。让我告诉你如何,

这样的界面,

export interface booleanReturn{
    retData: boolean;
}

在您的服务中导入此内容,并按照以下说明使用它,

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpBackend } from '@angular/common/http/src/backend';
import { booleanReturn } from '../interfaces/MyInterfaces';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class MyServiceService {

  constructor(private httpClient: HttpClient) { }

  getBoolean(): Observable<booleanReturn>{
    return this.httpClient.get<booleanReturn>('http://localhost:8080/getBoolean');
  }
}

现在在您的组件中执行类似的操作,

import { Component } from '@angular/core';
import { MyServiceService } from './my-service.service';
import { booleanReturn } from '../interfaces/MyInterfaces';

/*interface booleanReturn{
  retData: boolean
}*/
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  myObj ={"decision":'2==2'};
  private myBooleanVal : booleanReturn;
  constructor(private myService:MyServiceService){
    this.getBooleanValue();
  }
  myFunction(vwLog){
    //console.log(eval(vwLog),"Dj test");
    return eval(vwLog);
  }

  getBooleanValue(){
    this.myService.getBoolean().subscribe(x=>{
      this.myBooleanVal = x;
      console.log(x,"X Value");// this print true "X Value"
      console.log(this.myBooleanVal);// this prints "true"
    });
  }
}

请参阅下面的屏幕截图,我可以看到正确的答案

enter image description here

答案 1 :(得分:1)

我怀疑订阅发生时"this" is out of context

尝试将代码添加到构造函数中:

private isUScustomer: Boolean; 


constructor() {
  this.myservice.isUSCustomer()
              .subscribe(x => {
                   this.isUScustomer = x;
                   console.log(this.isUScustomer); //Its undefined 
                   console.log(x); //it logs true.
  });
}

答案 2 :(得分:1)

使用Let代替私有并返回该变量

答案 3 :(得分:0)

如果您使用布尔类型,则必须在设置之前使用默认值对其进行初始化。

private isUScustomer: Boolean = false; 

另一种选择是使用布尔类型

private isUScustomer: boolean;