combineLatest弃用了静态combineLatest

Sam*_*lib 32 rxjs rxjs6

使用后运行rxjs迁移工具

rxjs-5-to-6-migrate -p src/tsconfig.app.json

我现在得到一个linting错误:

combineLatest已弃用:不赞成使用static combineLatest.

在运行迁移命令之前,这是我的代码:

this.store.combineLatest(
        this.store.select(lang.getCurrent),
        this.store.select(lang.getCurrentLocale)
    ).subscribe(([state, currentLang, locale]) => {
        this._language = session.language === currentLang ? '' : currentLang;
        this._locale = session.locale === locale ? '' : locale;
    });
Run Code Online (Sandbox Code Playgroud)

运行迁移命令后的代码:(当前显示linting错误)

import {map, combineLatest} from 'rxjs/operators';
this.store.combineLatest(
        this.store.select(lang.getCurrent),
        this.store.select(lang.getCurrentLocale)
    ).subscribe(([state, currentLang, locale]) => {
        this._language = session.language === currentLang ? '' : currentLang;
        this._locale = session.locale === locale ? '' : locale;
    });
Run Code Online (Sandbox Code Playgroud)

问题在这个stackoverflow问题中被问到,但它不够具体:Angular 6 ng lint重复错误和警告,combineLatest已被弃用 .

Sam*_*lib 51

我在这篇文章中找到了一个答案:RxJS 6:什么是新的,什么改变了?(来自官方文档):

解决方案是转换:

import { combineLatest } from 'rxjs/operators';

a$.pipe(combineLatest(b$, c$));
Run Code Online (Sandbox Code Playgroud)

成:

import { combineLatest } from 'rxjs';

combineLatest(a$, b$, c$);
Run Code Online (Sandbox Code Playgroud)

  • 或者如果您想在管道中保留CombineLatest,则可以执行`a $ .pipe(a $ => CombineLatest(a $,b $,c $));` (5认同)
  • 我使用的是rxjs版本6.5.3和idea Ultimate 2020.1.2。并按照rxjs官方文档中的建议从“rxjs”而不是“rxjs/operators”导入combineLatest,但它仍然被intellij标记为已弃用。真他妈烦人! (4认同)
  • 看来这个解决方案现在也已被弃用。新的新方式是什么? (3认同)
  • 我更新了答案。如果弗里德曼的答案已经过时,请告诉我们。 (2认同)
  • @ParrapbanzZ 接受多个可观察值的版本确实已被弃用。您应该传入一个可观察值数组。例如,“combineLatest(a$, b$)”而不是“combineLatest([a$, b$])” (2认同)

ofi*_*man 32

在rxjs 6.5中

import { combineLatest } from 'rxjs';
combineLatest([a$, b$, c$])

Run Code Online (Sandbox Code Playgroud)

  • 当对象已经是流数组时,为什么要映射并返回对象? (3认同)
  • 这将创建一个 Observable<[typeof(a), typeof(b), typeof(c)]> 而不是单个组合类型 (2认同)

小智 5

rxjs 版本 6.4.0

您应该从 RxJs 运算符导入映射运算符才能使其工作

combineLatest(a$, b$, c$).pipe(map([a, b, c]) => treat(a, b, c))
Run Code Online (Sandbox Code Playgroud)

  • 虽然此代码可以回答问题,但提供有关如何和/或为何解决问题的额外上下文将提高​​答案的长期价值。[阅读本文](https://stackoverflow.com/help/how-to-answer )。 (4认同)
  • 然而,这是这里最完整的答案。所有其他答案都返回与预期类型不同的可观察类型,这需要应用此处理。它说得最少,但提供的却最多。 (2认同)

Owe*_*vin 5

似乎已被弃用,并且按照各种答案中的建议传递数组仍然会在 rxjs v.7.0.0弃用通知combineLatest中引发错误

替换为combineLatestWith. 将在 v8 中删除。

对于使用 rxjs v.7.0.0 及以上版本的用户,您将需要使用combineLatestwith

input1Changes$.pipe(
  combineLatestWith(input2Changes$)
)
Run Code Online (Sandbox Code Playgroud)