我对angular还很陌生,我使用的是Angular 15,我基本上有一个Rest API响应,我想使用angular解析并在UI中显示。为此,我使用了HttpClient
和GET
请求来解析响应。app.component.ts
的代码为:
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent{
constructor(private http: HttpClient){}
posts: any[] = [];
loadPosts(){
this.http
.get('https://jsonplaceholder.typicode.com/todos/1')
.subscribe((posts: any[])=>{
this.posts=posts;
});
}
}
EDIT我更改了get请求调用的链接,以便可以实际使用它。
app.component.html
的代码为:
This is a template for app component html
<br>
<button type="button" (click)="loadPosts()" class="btn btn-primary">Get Response Body</button>
<br>
<div *ngFor="let post of posts">
<h1>{{ posts.includes }}</h1>
<p> {{ posts.keys }} </p>
</div>
<router-outlet></router-outlet>
我试图从响应中获取某些值,比如来自API调用的includes
和keys
,我想解析这些值并在UI中显示。这就是为什么我使用ngFor迭代响应并只显示这些部分。
我的主要问题是我得到这个错误:
src/app/app.component.ts:19:16 - error TS2769: No overload matches this call.
Overload 1 of 3, '(observer?: Partial<Observer<Object>> | undefined): Subscription', gave the following error.
Type '(posts: any[]) => void' has no properties in common with type 'Partial<Observer<Object>>'.
Overload 2 of 3, '(next: (value: Object) => void): Subscription', gave the following error.
Argument of type '(posts: any[]) => void' is not assignable to parameter of type '(value: Object) => void'.
Types of parameters 'posts' and 'value' are incompatible.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Type 'Object' is missing the following properties from type 'any[]': length, pop, push, concat, and 29 more.
Overload 3 of 3, '(next?: ((value: Object) => void) | null | undefined, error?: ((error: any) => void) | null | undefined, complete?: (() => void) | null | undefined): Subscription', gave the following error.
Argument of type '(posts: any[]) => void' is not assignable to parameter of type '(value: Object) => void'.
Types of parameters 'posts' and 'value' are incompatible.
Type 'Object' is not assignable to type 'any[]'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
19 .subscribe((posts: any[])=>{
~~~~~~~~~~~~~~~~~
使用[]
In时
.get('https://cdn.contentful.com/myCustomApiLink')
.subscribe((posts: any[])=>{
this.posts=posts;
但如果不使用[]
,则会出现错误:
app.component.ts:19 ERROR Error: NG0900: Error trying to diff '[object Object]'. Only arrays and iterables are allowed
at DefaultIterableDiffer.diff (core.mjs:28817:19)
at NgForOf.ngDoCheck (common.mjs:3213:42)
at callHook (core.mjs:2758:18)
at callHooks (core.mjs:2717:17)
at executeCheckHooks (core.mjs:2649:5)
at refreshView (core.mjs:12084:21)
at refreshComponent (core.mjs:13208:13)
at refreshChildComponents (core.mjs:11865:9)
at refreshView (core.mjs:12125:13)
at detectChangesInternal (core.mjs:13352:9)
load (async)
loadPosts @ app.component.ts:19
AppComponent_Template_button_click_2_listener @ app.component.html:3
Show 82 more frames
按钮关闭对话框.
任何帮助都是感激不尽的!谢谢!
2条答案
按热度按时间5lhxktic1#
必须键入
get
方法我建议根本不要使用
any
vxbzzdmp2#
由于API返回的是对象,因此响应类型与提及的类型不匹配。
所需的数组似乎是响应的一部分,因此请尝试在订阅之前通过管道传输响应,以仅获取所需的数组。