简体   繁体   English

我如何在 rxjs 订阅函数中传递值?

[英]how can i pass value in rxjs subscribe function?

I'm working on switching the code to rxjs我正在将代码切换到 rxjs

here is my original code.这是我的原始代码。

userAuth$: BehaviorSubject<ArticleInfoRes>;

  async loadArticleList(articleId: number) {
    try {
      const data = await this.articleApi.loadArticleList(articleId);
      this.userAuth$.next(data);
      return data;
    } catch (error) {
      return error;
    }
  }

Subsequently, an attempt was made to convert to rxjs, but the value was not delivered properly.随后,尝试转换为 rxjs,但未正确传递值。 A value or error must be passed to the location where the loadArticleList function is used.必须将值或错误传递到使用 loadArticleList 函数的位置。 Please tell me what's wrong请告诉我怎么了

This is the code I tried to convert.这是我尝试转换的代码。

userAuth$: BehaviorSubject<ArticleInfoRes>;

loadArticleList(articleId: number) {
  from(this.articleApi.loadArticleList(articleId)).subscribe(
    data => {
      this.userAuth$.next(data);
      return data;
    },
    error => {
      return error;
    }
  )
}

A value or error must be passed to the location where the loadArticleList function is used.必须将值或错误传递到使用 loadArticleList 函数的位置。 Please tell me what's wrong请告诉我怎么了

To answer your question directly, the reason why the result (ie, value or error) isn't passed to where you use loadArticleList() is because your RxJS pipeline is not built properly.要直接回答您的问题,结果(即值或错误)未传递到您使用loadArticleList()的原因是因为您的 RxJS 管道未正确构建。 For one, adding return data;一是添加return data; and return error;return error; in your subscription handlers won't actually return data nor error .在您的订阅处理程序中实际上不会返回dataerror

To get the result, you might want to "trickle down" (streamline) that result further down the RxJS pipeline, so that subscription to that result happens in the location you speak of, to where loadArticleList() is used.要获得结果,您可能希望在 RxJS 管道中进一步“涓涓细流”(流线化)该结果,以便对该结果的订阅发生在您所说的位置,即使用loadArticleList()的位置。 This translates to moving your call to .subscribe() outside of loadArticleList() , not inside.这意味着将您的调用移动到.subscribe()之外loadArticleList() ,而不是内部。

So, here's a proper RxJS revision:所以,这是一个正确的 RxJS 版本:

import { tap } from "rxjs/operators";

userAuth$: BehaviorSubject<ArticleInfoRes>;

loadArticleList(articleId: number) {
  return from(this.articleApi.loadArticleList(articleId)).pipe(
    tap((data) => this.userAuth$.next(data)) // 👈 tap into the result to update the next userAuth$
  );
}

Using the tap RxJS operator, we can grab the result of loadAarticleList(articleId) , process it according to your callback function inside tap (in this case, simply update the next value of userAuth$ ), and then with that same grabbed result, unmodified, pass it on to whoever subscribes to loadArticleList() .使用tap RxJS 操作符,我们可以获取loadAarticleList(articleId)的结果,根据你在tap内部的回调函数处理它(在这种情况下,只需更新userAuth$的下一个值),然后使用相同的抓取结果,未修改,将其传递给订阅loadArticleList()的任何人。

Finally, to actually "pass" data or error to the location where the loadArticleList function is used, simply subscribe there.最后,要将dataerror实际“传递”到使用loadArticleList函数的位置,只需在此处订阅即可。

// in your component's .ts somwehere, probably
loadArticleList(123).subscribe(
  data  => { /* do as you want with the result     */ },
  error => { /* add your error-handling logic here */ }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM