如何从服务获取 component.ts 中的可见数据(可观察模式)

How to Visible data in component.ts getting from Service ( Observable pattern )

服务.ts

 get_update(id:any): Observable<any[]>{
    let headers = new Headers({ 'Content-Type': 'application/json'});
    let options = new RequestOptions({ headers: headers });
    return this.http.get('http://localhost:8000/vendor/'+id,options)
                        .map(response => response.json())
                        .catch(error => Observable.throw(error.statusText));       
}
component.ts

ngOnInit()

{ this.service.get_update(this.user).订阅(数据=> console.log(数据));

}

我想将数据存储到另一个变量中,我想转换成全局变量

我猜你想记录响应? 你只需要

this.service.get_update(this.user).subscribe(update => console.log(update),
                                                      error => console.log(error));

您可以记录响应,也可以在模板中插入更新变量的值。

this.service.get_update(this.user).subscribe((response) => {
    this.varibleName = response;
    console.log(this.varibleName);
}, (error) => {
    console.log(error);
});

或在模板中

{{variableName | json}}

此外,还有一个使用异步管道的选项

   this.serviceData = this.service.get_update(this.user).map(res => res.json())

并在您的模板中

{{serviceData | async}}