我正在尝试将现有应用程序从使用更改Http为使用HttpClient,但是我有一个错误.
因此,在我的服务中,您现在可以看到新代码与已注释掉的旧代码:
constructor(
// private http: Http
private http: HttpClient
) { }
getSidebar() {
// return this.http.get('http://localhost:3000/sidebar/edit-sidebar')
// .map(res => res.json());
return this.http.get('http://localhost:3000/sidebar/edit-sidebar');
}
Run Code Online (Sandbox Code Playgroud)
在我,page.component.ts我有这个
this.sidebarService.getSidebar().subscribe(sidebar => {
this.sidebar = sidebar.content; // this does not work now
});
Run Code Online (Sandbox Code Playgroud)
但是对于我评论过的行,我现在得到了这个错误:
Property 'content'
does not exist on type 'Object'.
Run Code Online (Sandbox Code Playgroud)
但是,如果我console.log(sidebar)得到以下内容:
{_id: "59dde326c7590a27a033fdec", content: "<h1>sidebar here</h1>"}
Run Code Online (Sandbox Code Playgroud)
那么问题是什么?
再一次,Http有效,但HttpClient没有.