在Angular RXJS中添加可观察到的可观察返回的部分数据

发布于 2025-02-09 16:00:54 字数 1213 浏览 2 评论 0原文

在我的代码中,我需要使用计时器(500)添加延迟。但是问题是它返回部分数据。它正在返回2个字段,而实际数据有17个字段。我附上了代码。请看看。谢谢

返回的价值:

 ['booking_display_id', 'edit']

期望值:

 ['booking_display_id', 'bookingstatus', 'b_contactname', 'member', 'b_emailaddress', 'b_mobilenumber', 'startdate', 'enddate', 'duration', 'bookingguest', 'guestnotes', 'vouchers', 'paypalpaymentpdt', 'totalCost', 'canPay', 'canCancel', 'edit']

 this.displayedColumns = combineLatest(this.table.columns.reduce((observables: Observable<boolean>[], col) => {
  // handle showIf property of column
  const show = col.showIf(this.injector, this.route.queryParamMap);
  observables.push(show instanceof Observable ? show : of(show));
  return observables;
}, []), timer(500)).pipe(
  map(showCols => {
    const cols = this.table.columns.filter((c, i) => showCols[i])
      .map(c => c.id);
    this.editEnabled && cols.push('edit');
    this.deleteEnabled && cols.push('delete');
    console.log('cols', cols)
    return cols;
  })
 );

In my code I need to add delay by using timer(500). But the issue is that it returns partial data. It is returning 2 fields while actual data has 17 fields. I have attached my code. please see it. Thank you

Returned value:

 ['booking_display_id', 'edit']

Expected value:

 ['booking_display_id', 'bookingstatus', 'b_contactname', 'member', 'b_emailaddress', 'b_mobilenumber', 'startdate', 'enddate', 'duration', 'bookingguest', 'guestnotes', 'vouchers', 'paypalpaymentpdt', 'totalCost', 'canPay', 'canCancel', 'edit']

 this.displayedColumns = combineLatest(this.table.columns.reduce((observables: Observable<boolean>[], col) => {
  // handle showIf property of column
  const show = col.showIf(this.injector, this.route.queryParamMap);
  observables.push(show instanceof Observable ? show : of(show));
  return observables;
}, []), timer(500)).pipe(
  map(showCols => {
    const cols = this.table.columns.filter((c, i) => showCols[i])
      .map(c => c.id);
    this.editEnabled && cols.push('edit');
    this.deleteEnabled && cols.push('delete');
    console.log('cols', cols)
    return cols;
  })
 );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请帮我爱他 2025-02-16 16:00:54

您的问题是,您通过了一系列可观察到的东西,以及另一个可观察到的combineLatest。但是,它只需要一系列可观察到的(1维数组)。因此,您需要用另一个combineLatest包裹您的减少,或者将它们传播(=&gt; [... obs,timer] obs obs将是您的降低)。

这是您简化的当前调用,

combineLatest([
    [of(1), of(2), of(3)],
    timer(500)
]);

我建议您使用传播操作员构建一个新数组,或者您可以使用另一个combineLatest,因为它是另一个可观察到的数组。


另外,我建议您使用delay而不是计时器

如果您希望将值与计时器分开, 则仅运行一次)
bombineLatest([combineLatest(.. obs),计时器])

combineLatest([
    combineLatest([of(1), of(2), of(3)]),
    timer(500)
]).pipe(
    map(([yourValues, timer]) => {
        // ...
    })
)

如果您不希望将值与计时器分开
CombineLatest([.. obs,timer])

combineLatest([
    ...[of(1), of(2), of(3)],
    timer(500)
]).pipe(
    map(([yourValuesWithTimer]) => {
        // ...
    })
)

Your issue is that you pass an array of observables as well as another observable to the combineLatest. However it only takes an array of observables (1dimensional array). Therefor you need to either wrap your reduce with another combineLatest or spread them ( => [...obs, timer] obs would be your reduce) into one array.

This is your simplified current call

combineLatest([
    [of(1), of(2), of(3)],
    timer(500)
]);

I would recommend you use the spread operator to build a new array like so or you can use another combineLatest since it is another array of observables.


Also I recommend that you use delay instead of timer (delay runs only once)

If you want your values to be separated from the timer
combineLatest([combineLatest(..obs), timer])

combineLatest([
    combineLatest([of(1), of(2), of(3)]),
    timer(500)
]).pipe(
    map(([yourValues, timer]) => {
        // ...
    })
)

If you do not want your values to be separated from the timer
combineLatest([..obs, timer])

combineLatest([
    ...[of(1), of(2), of(3)],
    timer(500)
]).pipe(
    map(([yourValuesWithTimer]) => {
        // ...
    })
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文