Angular typescript http客户端无法分析布尔响应
HttpClient未解析布尔值 服务Angular typescript http客户端无法分析布尔响应,angular,typescript,angular-services,Angular,Typescript,Angular Services,HttpClient未解析布尔值 服务 public isUSCustomer(): Observable<Boolean> { return this.httpClient .get<Boolean>(myurl); } 输出 console.log(this.isUScustomer); undefined console.log(x); //it logs true. console.log(typeof x); boolean 我试过
public isUSCustomer(): Observable<Boolean> {
return this.httpClient
.get<Boolean>(myurl);
}
输出
console.log(this.isUScustomer); undefined
console.log(x); //it logs true.
console.log(typeof x); boolean
我试过像这样使用接口Boolean
和Boolean
return this.httpClient
.get<boolean>(myurl);
and
return this.httpClient
.get<Boolean>(myurl);
我读过这篇文章,但那是2013年
版本信息
打字稿:2.4.2Angular:5.0.2几天前我看到了一个相同的错误,我偶然发现了一个叫做eval函数的东西,这就是你可以尝试运行这个函数的方式
private isUScustomer: Boolean;
this.myservice.isUSCustomer()
.subscribe(x => {
this.isUScustomer = eval(x);
});
编辑1
TS查找http调用的返回类型和数据类型,您可以尝试创建一个接口并尝试一下。让我告诉你怎么做
像这样的界面,
export interface booleanReturn{
retData: boolean;
}
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"
});
}
}
将其导入到您的服务中并按如下所示使用
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');
}
}
请看下面的截图,我可以看到正确的答案
我怀疑当订阅发生时
尝试将代码添加到构造函数中:
private isUScustomer: Boolean;
constructor() {
this.myservice.isUSCustomer()
.subscribe(x => {
this.isUScustomer = x;
console.log(this.isUScustomer); //Its undefined
console.log(x); //it logs true.
});
}
使用
让
而不是private并返回该变量如果您使用的是布尔型类型,则必须在设置之前使用默认值对其进行初始化
private isUScustomer: Boolean = false;
另一种选择是使用布尔类型
private isUScustomer: boolean;
console.log(typeof x)
返回什么?它记录了booleanWell,这很奇怪。首先,不要使用接口,只使用基元类型。您可以尝试使用this.isUScustomer=x吗?真:假代码>?console.log(x)返回什么?是真还是“真”?返回真值让我试试,但我有一个服务请求评估get(url)。应该解析为布尔值是的,但是,试一试,我可以用它来解决布尔值的问题。在我的例子中,它是一个错误或字符串类型和布尔值类型,不知何故,我认为由于动态强制转换,它不起作用,并且即使被称为布尔值也接受为字符串。@Eldho运气好吗?请不要进行评估。被认为是有害的。我不认为这是因为隐私。因为我使用的其他服务与复杂对象类似,工作正常。我将尝试此操作,但我的im在服务中使用的不同方法与复杂对象相同,工作正常。private isUScustomer:Boolean=false应该有效。。最终的客户价值是多少;但我没有设定一个价值;我刚刚定义了像isuscustomer:boolean这样的属性;
private isUScustomer: Boolean = false;
private isUScustomer: boolean;