rxJS可观察到未达到订阅

编程入门 行业动态 更新时间:2024-10-22 10:51:30
本文介绍了rxJS可观察到未达到订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用Angular 2和RxJS,并且很难建立一个简单的Observable系统.

I'm with Angular 2 and RxJS and I'm having a hard time setting up a simple observables system.

据我了解,operator do用于产生副作用,并且您将代码处理可观察对象返回的结果放在susbcribe()函数中.

As far as I understand, operator do is used for side effects, and you place the code to deal with the results returned by the observable in the susbcribe() function.

因此,我的组件要求服务初始化系统. 该服务向服务器发出2个http调用,并将它们合并在一个流中,以供组件进行预订并确保一切准备就绪. 服务中的2个http调用之一正在从服务器检索固定数量的结果,然后将这些结果按需提供给Component.因此,当服务发现需要更多数据时,它将再次调用服务器.

So my Component ask the Service to initialize the system. The Service makes 2 http calls to the server and combines them both in a single stream for the component to subscribe and make sure everything is ready. One of the 2 http calls in the service is retrieving a fixed number of results from the server, which will be then served to the Component on demand. So when the Service sees that it needs more data, it makes another call to the server.

这不起作用,因为我认为我需要先取消订阅原始的http调用,然后再进行新的调用(是真的吗?).

This was not working because I think I need to unsubscribe first to the original http call before making a new one (is that true?).

这就是我所拥有的...无法正常工作,因为它无法在getQuestions方法中进行订阅.

So this is what I've got... which is not working because it not getting to subscribe in the getQuestions method.

在组件中

ngOnInit() { // show spinner this.gamesService .initializeGame() .subscribe(data => { console.log('Game initialized'); // remove spinner }); }

在服务中

initializeGame(opponent_id: string): Observable<any> { return Observable .forkJoin([this.getQuestions(), this.newGame()]); } getQuestions(): Observable<any> { // composes postData with POST parameters this.questionsObservable = this.authHttp .post(this.settings.authApiUrl, postData) .map((response: Response) => response.json()); this.questionsObservable .subscribe(data => { // save data in the service, which will be consumed }) .unsubscribe(); // do I need to unsubscribe? return this.questionsObservable; } getQuestion(category: string): any { // consume question by Component if (no_more_questions_in_service) this.getQuestions(); return question; }

因此,这现在正在工作. 为什么没有达到订阅方法?

So, this is now working. Why is not reaching the subscribe method?

是否有更好的方法来实现这一目标? 谢谢

Is there a better way to achieve this? Thanks

推荐答案

好,我知道(也要感谢@martin)我是在时间之前退订的.

Ok, I realize (also, thanks @martin) I was unsubscribing before time.

我设法得到了想要的东西,以防万一有人想要使用它.

I managed to get what I wanted, so in case someone wants to use it.

该服务现在是这样的:

initializeGame(opponent_id: string): Observable<any> { return Observable .forkJoin([this.getQuestions(), this.newGame()]); } getQuestions(): Observable<any> { // postData return this.authHttp .post(this.settings.authApiUrl, postData) .map((response: Response) => response.json()) .do(data => save_data); } private getQuestionsByCategory(category: string) { // postData this.authHttp .post(this.settings.authApiUrl, postData) .map((response: Response) => response.json()) .subscribe(data => save_data } getQuestion(category: string): any { // consume question by Component if (no_more_questions_in_service) this.getQuestionsByCategory(category); } return question; }

所以我现在有2种不同的方法;我不需要退订(我认为),而且现在似乎运作良好.

So I have 2 different methods now; I don´t need to unsubscribe (I think) and it seems to be working fine now.

更多推荐

rxJS可观察到未达到订阅

本文发布于:2023-10-27 18:48:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534232.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:观察到   rxJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!