响应状态 - Angular 5
Status of the response - Angular 5
我在 base.service
上创建了一个简单的 GET
,以便在 URL 是否有效时得到响应。
/** Check connection */
checkConnection(url: string) {
return this.http.get(url);
}
我想用我的组件之一的 URL
调用它并读取响应状态:
checkConnection(protocol, host, port) {
const url = protocol.toLowerCase().concat("://").concat(host).concat(":").concat(port);
this.baseService.checkConnection(url)
.subscribe(
response => {
let status = response.status;
},
(err) => console.log(err)
);
}
但是我收到一个错误
Porperty status does not exist on type 'Ojbect'
所以编译不了
您的问题与打字稿本身有关,因为响应在您的代码中没有明确的类型。
假设您使用的是 HttpClient,根据文档,get observable 的类型不明确 (https://angular.io/api/common/http/HttpClient#get)。
一个简单的解决方案是使用任何:
.subscribe(
(response: any) => {
let status = response.status;
},
我在 base.service
上创建了一个简单的 GET
,以便在 URL 是否有效时得到响应。
/** Check connection */
checkConnection(url: string) {
return this.http.get(url);
}
我想用我的组件之一的 URL
调用它并读取响应状态:
checkConnection(protocol, host, port) {
const url = protocol.toLowerCase().concat("://").concat(host).concat(":").concat(port);
this.baseService.checkConnection(url)
.subscribe(
response => {
let status = response.status;
},
(err) => console.log(err)
);
}
但是我收到一个错误
Porperty status does not exist on type 'Ojbect'
所以编译不了
您的问题与打字稿本身有关,因为响应在您的代码中没有明确的类型。
假设您使用的是 HttpClient,根据文档,get observable 的类型不明确 (https://angular.io/api/common/http/HttpClient#get)。
一个简单的解决方案是使用任何:
.subscribe(
(response: any) => {
let status = response.status;
},