我刚开始学习Angular 2我正在尝试使用get调用来访问URL,但是即使在浏览器的网络中,get似乎也没有通过我无法找到获取URL的URL.
该程序将进入上面和下面的方法控制台记录,但是get调用没有任何内容
我的服务方式
import { Headers, Http, Response } from '@angular/http';
import { Injectable } from '@angular/core';
import { Persons } from './mock-people';
import { Person } from './person';
import {Observable} from 'rxjs/Rx';
getAllPersons(): void {
console.log("Here");
this.http.get(`http://swapi.co/api/people/1`)
.map((response: Response) => {
console.log(response.json());
response.json();
});
console.log("Comes here 2");
}
Run Code Online (Sandbox Code Playgroud)
进口HttpModule在app.module.ts
我的控制台屏幕截图
![安慰]](https://i.stack.imgur.com/w9AeE.png)
Mil*_*lad 94
Http使用rxjs并且是一个冷/懒惰的可观察对象,这意味着你应该订阅它以使它工作.
this.http.get(`http://swapi.co/api/people/1`)
.map((response: Response) => {
console.log(response.json());
response.json();
})
.subscribe();
Run Code Online (Sandbox Code Playgroud)
或者如果您想从其他地方订阅,您应该返回如下http.get方法:
getAllPersons(): Observable <any> {
console.log("Here");
return this.http.get(`http://swapi.co/api/people/1`)
.map((response: Response) => {
console.log(response.json());
return response.json();
});
}
Run Code Online (Sandbox Code Playgroud)
然后 :
getAllPersons().subscribe();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41755 次 |
| 最近记录: |