在Angular 2中使用subscribe获取http响应后获取[object object]

在Angular 2中使用subscribe获取http响应后获取[object object],angular,angularjs-directive,angular2-routing,angular-services,Angular,Angularjs Directive,Angular2 Routing,Angular Services,ProductService.ts getProduct(id: number): Observable<IProduct> { return this._http.get(this._productUrl + '/GetById/' + id).map((response: Response) => <IProduct>response.json()) .catch(this.error

ProductService.ts

   getProduct(id: number): Observable<IProduct> {

    return this._http.get(this._productUrl + '/GetById/' + 
           id).map((response: Response) => <IProduct>response.json())           
        .catch(this.errorHandler);     
}
getProduct(id:number):可观察{
返回此
map((response:response)=>response.json())
.catch(this.errorHandler);
}
ProductDetailComponent.ts

   getProduct(id: number) {
    this._productService.getProduct(id).subscribe(
        res => {               
            console.log('before component ' + res);     
            this.product = res;
            console.log('after component ' + res);                
        },
        error => this.errorMessage = <any>error),
        console.log('execution complete'); 
}
getProduct(id:number){
此.\u productService.getProduct(id).订阅(
res=>{
console.log('before component'+res);
本产品=res;
console.log('在组件'+res之后);
},
error=>this.errorMessage=error),
console.log(“执行完成”);
}
当在subscribe中收到结果时,它将显示为
执行完毕, 在组件[对象]之前,
在组件[对象对象]之后

需要执行JSON.stringify

console.log('before component ' + JSON.stringify(res));  

您需要执行JSON.stringify

console.log('before component ' + JSON.stringify(res));  

您正在使用字符串连接(使用
+
符号)。这样,Javascript将首先将对象转换为字符串(
[Object Object]
)。如果您不想这样做,可以这样尝试:
console.log('before component',res)


请注意逗号,而不是
+
。这会将对象作为单独的参数传递到
console.log
,允许浏览器或CLI进行渲染。例如,通过这种方式(在浏览器中)可以展开或折叠对象。

您正在使用字符串连接(使用
+
符号)。这样,Javascript将首先将对象转换为字符串(
[Object Object]
)。如果您不想这样做,可以这样尝试:
console.log('before component',res)


请注意逗号,而不是
+
。这会将对象作为单独的参数传递到
console.log
,允许浏览器或CLI进行渲染。例如,通过这种方式(在浏览器中)可以展开或折叠对象。

还可以执行以下操作:

console.log('before component',JSON.stringify(res,undefined,2))

要将
[Object Object]
漂亮地打印为json对象。

您还可以执行以下操作:

console.log('before component',JSON.stringify(res,undefined,2))

[Object Object]
漂亮地打印为json对象。

console.log('before component'+json.stringify(res))@Shota do
console.log(JSON.stringify(res,undefined,2))
to pretty printThank,console.log('before component',res);也是另一个选项。还有,为什么要在前后打印
res
?它的值看起来没有改变?console.log('before component'+JSON.stringify(res))@Shota do
console.log(JSON.stringify(res,undefined,2))
to pretty printThank,console.log('before component',res);也是另一个选项。还有,为什么要在前后打印
res
?它的值看起来并没有改变?但我仍然无法在这个.product对象中获得期望的结果。组件{“productId”:1,“productName”:“Garden Cart”,“productCode”:“GDN-0023”,“发布日期”:“2016年3月18日”,“价格”:32.99,“说明”:“15加仑容量滚动花园车”,“Starting”:4.2,“imageUrl”:“app/Images/a.png”}产品详细信息。组件[object]后的组件.ts:38console.log('after component'+this.product);你得到了什么?你期待什么?在alsoi之后使用JSON.stringify打印需要将JSON结果绑定到产品对象,正如我所说的使用
console.log('after component',JSON.stringify(this.product,undefined,2))
但在执行此操作时,this.product=JSON.stringify(this.product,undefined,2);我得到了这个错误“string不可分配给类型ipproduct”,但我仍然无法在这个.product对象中得到所需的结果。在组件{“productId”:1,“productName”:“Garden Cart”,“productCode”:“GDN-0023”,“releaseDate”:“2016年3月18日”,“price”:32.99,“description”:“15加仑容量滚动花园车”,“starRating”:4.2,“imageUrl”:“app/Images/a.png”}产品详细信息。component.ts:38在component[object]console.log之后('after component'+this.product);您得到了什么,期望得到什么?在also之后使用JSON.stringify打印我需要将JSON结果绑定到产品对象,正如我所说的使用
console.log('after component',JSON.stringify(this.product,未定义,2))
但在执行此操作时,this.product=JSON.stringify(this.product,未定义,2);我收到以下错误“string不可分配给IPProduct类型”