rxJS可观察到未达到订阅 [英] rxJS observable not reaching subscribe

查看:264
本文介绍了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可观察到未达到订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆