【发布时间】:2018-02-01 14:54:48
【问题描述】:
上面的错误是我在“编译”后在命令行中得到的(指 post 请求中 subscribe 方法的第二次调用。非常简单),我订阅了一个 observable 以获得对http 请求。没有服务(至少是我创建的)被导入到其他文件夹中,据说 post 方法工作所需的一切。此外,我还设置了模拟 API,邮递员可以成功地向/从该 API 发出 get 和 post 请求。
import { Component, OnInit } from '@angular/core';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import {Observable} from 'rxjs/Observable';
@Component({
selector: 'simple-http',
templateUrl: 'simple-http.component.html'
})
export class SimpleHttpComponent implements OnInit{
data: Object;
loading: boolean;
constructor(public http: Http) {
}
ngOnInit() {
}
getUser(): Object {
this.loading = true;
this.http.get('http://demo1495487.mockable.io/cort')
.subscribe((res: Response) => {
this.data = res.json();
this.loading = false;
console.log(this.data);
});
return this.data;
}
addUser(): void {
this.loading = true;
this.http.post('http://greenmachine.mockable.io/cortfield',
JSON.stringify({
body: 'bar',
title: 'foo',
userId: 1
}))
.suscribe((res: Response) => {
this.data = res.json();
console.log(this.data);
this.loading = false;
});
}
}
【问题讨论】:
-
不订阅就是订阅
标签: angular rest web-services http typescript