[英]Type 'Observable<any>' is not assignable to type 'JSON' in angular2 with rxjs5
I want to change all of my EventEmitter to Subjects since it is the recommended way to share variables and states instead of emitting an event. 我想将所有EventEmitter更改为Subject,因为这是共享变量和状态而不是发出事件的推荐方法。
But now I've got a problem and I just don't understand how to solve it. 但是现在我有一个问题,我只是不知道如何解决。 Here is the sample code:
这是示例代码:
@Injectable()
export class TicketsService implements OnInit {
private _url = "http://127.0.0.1:3000/api/getAllTickets";
private _refreshTickets:Subject<JSON> = new Subject<JSON>();
public allTickets:JSON;
private _url_postNewTicket = "http://127.0.0.1:3000/api/postNewTicket";
constructor(private _http:AuthHttp) {
this.allTickets = this._refreshTickets
.mergeMap(() => this._http.get(this._url))
.map((res:Response) => res.json())
.cache();
}
refreshAllTickets() {
console.log("refresh all tickets..");
this._refreshTickets.next(null);
}
}
The compiler gives me the following error: 编译器给我以下错误:
public/angular/app/tickets.service.ts(26,9): error TS2322: Type 'Observable' is not assignable to type 'JSON'.
public / angular / app / tickets.service.ts(26,9):错误TS2322:无法将类型“可观察”分配给类型“ JSON”。 [1] Property 'parse' is missing in type 'Observable'.
[1]类型“可观察”中缺少属性“分析”。
I guess I could write allTickets:JSON
as allTickets:any
but in the component I use ngFor to iterate - so I get the error from ngFor that it needs an array. 我想我可以将
allTickets:JSON
编写为allTickets:any
但是在组件中,我使用ngFor进行迭代-因此我从ngFor中得到了一个错误,它需要一个数组。
Thanks in advance! 提前致谢!
您可以将allTickets
更改为Observable<any>
类型,然后在ngFor绑定中使用如下所示的异步管道:
*ngFor="let ticket of allTickets | async"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.