Angular 5 HTTP 获取响应错误 属性 'body' 在类型 'Object' 上不存在
Angular 5 HTTP get response error Property 'body' does not exist on type 'Object'
我正在使用 Http 开发 Angular 5。
在服务应用程序时出现此错误..
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/switchMap';
@Component({
selector: 'app-post-detail',
templateUrl: './post-detail.component.html',
styleUrls: ['./post-detail.component.css']
})
export class PostDetailComponent implements OnInit {
body: string;
recent: {};
constructor(
private route: ActivatedRoute,
private router: Router,
private http: HttpClient,
) { }
ngOnInit() {
let id = this.route.snapshot.paramMap.get('id');
let userId = this.route.snapshot.paramMap.get('userid');
this.getUserPosts(userId);
this.http.get('https://jsonplaceholder.typicode.com/posts/'+id)
.subscribe((data)=>{
this.body = data.body;
});
}
}
我不知道为什么会出现此错误,我根据语法在 class 中将类型指定为字符串。
API 的回复如下,
(data)=>{
this.body = data.body;
}
data
不是 json。您需要在 data
上 运行 .json()
。或者您可以 运行 map
订阅前:
this.http.get('https://jsonplaceholder.typicode.com/posts/'+id)
.map(res => res.json())
.subscribe((data)=>{
this.body = data.body;
});
为了使用map
,您需要导入它:
import 'rxjs/add/operator/map'
在此处查看更多示例:https://angular.io/api/http/Http
编辑:
响应已键入 - 您需要为响应创建一个接口:
interface SomeResponse {
Body: SomeType;
}
并像这样提出请求:
this.http.get<SomeResponse>'https://jsonplaceholder.typicode.com/posts/'+id)
.subscribe((data)=>{
this.body = data.body;
});
同样,这里有更多示例:https://codingthesmartway.com/angular-4-3-httpclient-accessing-rest-web-services-with-angular/
我正在使用 Http 开发 Angular 5。
在服务应用程序时出现此错误..
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/switchMap';
@Component({
selector: 'app-post-detail',
templateUrl: './post-detail.component.html',
styleUrls: ['./post-detail.component.css']
})
export class PostDetailComponent implements OnInit {
body: string;
recent: {};
constructor(
private route: ActivatedRoute,
private router: Router,
private http: HttpClient,
) { }
ngOnInit() {
let id = this.route.snapshot.paramMap.get('id');
let userId = this.route.snapshot.paramMap.get('userid');
this.getUserPosts(userId);
this.http.get('https://jsonplaceholder.typicode.com/posts/'+id)
.subscribe((data)=>{
this.body = data.body;
});
}
}
我不知道为什么会出现此错误,我根据语法在 class 中将类型指定为字符串。
API 的回复如下,
(data)=>{
this.body = data.body;
}
data
不是 json。您需要在 data
上 运行 .json()
。或者您可以 运行 map
订阅前:
this.http.get('https://jsonplaceholder.typicode.com/posts/'+id)
.map(res => res.json())
.subscribe((data)=>{
this.body = data.body;
});
为了使用map
,您需要导入它:
import 'rxjs/add/operator/map'
在此处查看更多示例:https://angular.io/api/http/Http
编辑:
响应已键入 - 您需要为响应创建一个接口:
interface SomeResponse {
Body: SomeType;
}
并像这样提出请求:
this.http.get<SomeResponse>'https://jsonplaceholder.typicode.com/posts/'+id)
.subscribe((data)=>{
this.body = data.body;
});
同样,这里有更多示例:https://codingthesmartway.com/angular-4-3-httpclient-accessing-rest-web-services-with-angular/