简体   繁体   English

Angular httpClientModule

[英]Angular httpClientModule

I can´t handle res in array form by httpClientModule. 我无法通过httpClientModule处理数组形式的res。 The Api is created by NodeJS. Api由NodeJS创建。 A easy try looks like that: 一个简单的尝试如下所示:

res.send( [ { message: 'lego api!' } , { name: 'lucy' } ] ); res.send([{message:'lego api!'},{name:'lucy'}]);

The response works when i test it by my browser. 当我通过浏览器对其进行测试时,响应有效。 No i wants to use the same url in Angular. 不,我想在Angular中使用相同的网址。 For I imported the httpClientModule in app.module. 因为我在app.module中导入了httpClientModule。 The app component looks like that: 该应用程序组件如下所示:

import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

  ngOnInit(): void {
    console.log(this.list());
  }
  list(): Observable<any> {
    return this.http.get('http://localhost:8080/api').map(data => data.message);
  }

In console i only get the informations of the observable: 在控制台中,我仅获得可观察到的信息:

Observable {_isScalar: false, source: Observable, operator: MapOperator} operator : MapOperator {project: ƒ, thisArg: undefined} 可观察的{_isScalar:false,源:可观察的,运算符:MapOperator}运算符:MapOperator {项目:ƒ,thisArg:未定义}

source : Observable {_isScalar: false, source: Observable, operator: MapOperator} 来源:可观察{_isScalar:否,来源:可观察,操作员:MapOperator}

_isScalar : false _isScalar:假

proto : Object 原型 :对象

I dont get what i´m doing wrong. 我不明白我在做什么。

You need to subscribe to your Observable. 您需要订阅您的Observable。

An HttpClient method does not begin its HTTP request until you call subscribe() on the observable returned by that method. 直到您对由该方法返回的observable调用subscribe()为止,HttpClient方法才会开始其HTTP请求。

docs 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM