- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试将现有应用程序从使用 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');
}
在我的 page.component.ts
中我有这个
this.sidebarService.getSidebar().subscribe(sidebar => {
this.sidebar = sidebar.content; // this does not work now
});
但是,对于上面我评论的行,我现在收到此错误:
Property 'content'
does not exist on type 'Object'.
但是,如果我 console.log(sidebar)
我会得到以下内容:
{_id: "59dde326c7590a27a033fdec", content: "<h1>sidebar here</h1>"}
那么问题出在哪里呢?
再一次,Http
可以工作,但 HttpClient
却不能。
最佳答案
您可以使用接口(interface)、类等指定返回的类型。例如,您可以使用如下所示的内容:
return this.http.get<Sidebar>('http://localhost:3000/sidebar/edit-sidebar');
例如,侧边栏可能定义为:
interface Sidebar {
_id: string;
content: string;
}
参见Typechecking the response从 Angular 文档获取更多信息:
...TypeScript would correctly complain that the Object coming back from HTTP does not have a results property. That's because while HttpClient parsed the JSON response into an Object, it doesn't know what shape that object is.
关于Angular 4 - Http 到 HttpClient - 对象类型上不存在属性 'someproperty',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47484875/
这个问题不是重复的 这个问题与Java中“this”的含义无关。这是一个关于属性不同语法的问题,我试图根据我对另一种语言的理解来澄清。我一直在研究 android 文档,看到相同的属性有不同的引用,我
这个问题在这里已经有了答案: Spring Expression Language (SpEL) with @Value: dollar vs. hash ($ vs. #) (4 个答案) 关闭
我有一个类 Article,它具有 Image 类型的属性,如下所示: export class Article { public image:Image; public images:
在MVC ,有时我正在设置 ViewBag 的特定属性根据某些条件。例如: if(someCondition) { // do some work ViewBag.SomeProperty
我正在尝试这段代码,其中 someProperty().length 返回属性的长度,并且位于 foreach 循环内 但在 Firefox 中出现以下错误。 TypeError: someProp
如何扩展(或重载)包含集合中某些自定义属性的方法? 我希望该委托(delegate)将对象与其属性进行比较,如果存在具有相同属性值的对象则返回 true。 最佳答案 return myCollecti
我正在阅读 LazyTableImages code Apple 已经发布并且他们为此做了一些事情(在 NSOperation 子类中): - (void)dealloc { [myPrope
我正在尝试将现有应用程序从使用 Http 更改为使用 HttpClient,但出现错误。 因此,现在在我的服务中,您可以看到新代码与已注释掉的旧代码: constructor( //
我遇到了这个错误,我不知道问题出在哪里。 “userid”列在数据库中,在 bean 中。有人有什么主意吗? org.apache.jasper.JasperException: An excepti
我正在尝试将现有应用程序从使用 Http 更改为使用 HttpClient,但是我遇到了错误。 所以现在在我的服务中你可以看到新代码与被注释掉的旧代码: constructor( //
给定 class A { props: { bool?: boolean, test: string } = { test: 'a'
我是 Ember.js 的新手,阅读了它的文档并遵循了给定的起始示例。他们在计算属性中使用 property 方法并传递模型属性(有时使用 @each)。我不知道他们为什么使用它。这里的持续时间是使用
我在一些代码中创建了这个表达式: t => (t.SomeProperty As Object) 现在我试图通过这个小片段(很长一段时间从互联网上获得)来获取属性名称(SomeProperty):
例如,在 MVC 应用程序中,我可以使用 Html 助手来创建这样的标签: @Html.LabelFor(m => m.ProductName) 我没有在任何地方声明变量“m”,但 IDE 会自动找出
在我从 Spring boot 1.5 迁移到 2.x 之后,我遇到了以下问题: APPLICATION FAILED TO START Description: Failed to bind pro
我和我的一个同事都从事编程工作。他用 C# 创建了一个类,我正在努力将它转换为 VB.NET。除了一行之外,我得到了整个类的转换,在这一点上我无法弄清楚所以认为一双新的眼睛也许能够发现我的错误。 原始
假设我有这样的数据: class Location { public int Id { get; private set; } public string City { get; se
我是一名优秀的程序员,十分优秀!