【发布时间】:2021-04-30 16:20:18
【问题描述】:
我有以下函数从 Angular 9 服务返回数据:
@Injectable({
providedIn: 'root'
})
export class SecurityService {
constructor(private http: HttpClient) { }
public getFunds(): Observable<any> {
// could do a loader here, or in an interceptor - same for the post, put and delete
let data = this.http.get<any>("https://localhost:44337/security/").pipe(
map(response => response)
);
return data;
} }
服务按预期返回数据。我这样称呼它:
export class AppComponent {
funds$!: Observable<any>; // "!" turns off check to make sure vars are initialized, not good tho
constructor(private securityService: SecurityService) {}
ngOnInit() {
this.funds$ = this.securityService.getFunds();
this.securityService.getFunds().subscribe(f => (this.funds$ = f));
}
}
在我的 html 中,我尝试使用这样的数据:
<tr *ngFor="let f of funds$">
<td>{{ f.id }}</td>
<td>{{ f.name }}</td>
<td>{{ f.assets }}</td>
</tr>
我收到此错误:TS2322:类型“Observable”不可分配给类型“any[] |可迭代 | (可迭代 & 任何[]) | (任何[] & 可迭代) |空 |不明确的'。 类型 'Observable' 不可分配给类型 'any[] & Iterable'。 类型 'Observable' 不可分配给类型 'any[]'。
这在几年前曾经有效。显然 Angular 9 改变了一些东西......
【问题讨论】:
标签: angular