我發現了幾個具有相同標題的問題,據我所知,其中一些問題表明解決方案基本上是返回一個 Observable 而不是一個數組(其他問題是關于 FireBase 的,這不是我的情況)。好吧,就我而言,下面的代碼確實返回一個 Observable (查看“getServerSentEvent(): Observable {return Observable.create ...”)我的最終目標是獲取從 Rest WebFlux 返回的流中的所有事件。我沒有通過后端,因為我很確定這個問題與 Angular 中的一些錯誤有關。最重要的是,我可以調試并查看事件是否正確地從 app.component.ts 傳送到 extratos$(參見下圖)。整條日志core.js:6185 ERROR Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe' at invalidPipeArgumentError (common.js:5743) at AsyncPipe._selectStrategy (common.js:5920) at AsyncPipe._subscribe (common.js:5901) at AsyncPipe.transform (common.js:5879) at Module.??pipeBind1 (core.js:36653) at AppComponent_Template (app.component.html:8) at executeTemplate (core.js:11949) at refreshView (core.js:11796) at refreshComponent (core.js:13229) at refreshChildComponents (core.js:11527)應用程序組件.tsimport { Component, OnInit } from '@angular/core';import { AppService } from './app.service';import { SseService } from './sse.service';import { Extrato } from './extrato';import { Observable } from 'rxjs';@Component({ selector: 'app-root', templateUrl: './app.component.html', providers: [SseService], styleUrls: ['./app.component.css']})export class AppComponent implements OnInit { //extratos: any; extratos$ : Observable<any>; constructor(private appService: AppService, private sseService: SseService) { } ngOnInit() { this.getExtratoStream(); } getExtratoStream(): void { this.sseService .getServerSentEvent("http://localhost:8080/extrato") .subscribe( data => { this.extratos$ = data; } ); }}
1 回答

斯蒂芬大帝
TA貢獻1827條經驗 獲得超8個贊
當您編寫此內容時observer.next(this.extratos);
,這意味著您在回調的參數this.extratos
中在組件端得到的內容,因此當您這樣做時,您實際上是在存儲. TypeScript 不會抱怨這一點,可能是因為當您像您一樣從頭開始構建時,它不夠智能來推斷類型。data
this.extratos$ = data;
extratos
Array
Observable
嘗試這個:
this.extratos$ = this.sseService .getServerSentEvent("http://localhost:8080/extrato");
并在模板中:<div *ngFor="let ext of extratos$ | async">
- 1 回答
- 0 關注
- 112 瀏覽
添加回答
舉報
0/150
提交
取消