【问题标题】:Status of the response - Angular 5响应状态 - Angular 5
【发布时间】:2018-04-18 13:56:02
【问题描述】:

我在我的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)
        );
}

但我收到了一个错误

“Ojbect”类型不存在属性状态

所以无法编译

【问题讨论】:

    标签: angular typescript http response


    【解决方案1】:

    您的问题与打字稿本身有关,因为响应在您的代码中没有明确的类型。

    假设您使用的是 HttpClient,根据文档,get observable 没有明确输入 (https://angular.io/api/common/http/HttpClient#get)。

    一个简单的解决方案是使用任何:

    .subscribe(
            (response: any) => {
                let status = response.status;
            },
    

    【讨论】:

      猜你喜欢
      • 2016-09-06
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 2017-07-06
      • 2018-09-07
      • 2018-09-13
      • 1970-01-01
      • 2016-12-27
      相关资源
      最近更新 更多