84

运行 rxjs 迁移工具后使用

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

我现在收到一个 linting 错误:

不推荐使用 combineLatest:不推荐使用静态 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;
    });

运行迁移命令后我的代码:(当前出现 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;
    });

这个问题是在这个stackoverflow问题中提出的,但它不够具体:Angular 6 ng lint duplicate errors and warnings, combineLatest is deprecated

4

4 回答 4

115

在 rxjs 6.5+

import { combineLatest } from 'rxjs';

combineLatest([a$, b$, c$]);

对于大多数应用程序,将 observables 数组映射到一个新值也很有帮助:

combineLatest([a$, b$, c$]).pipe(
  map(([a$, b$, c$]) => ({
    a: a$, 
    b: b$, 
    c: c$
  }))
);

另见:https ://www.learnrxjs.io/learn-rxjs/operators/combination/combinelatest

于 2019-05-19T14:05:54.267 回答
88

已弃用!

请参阅fridman 的答案以获取 RxJs 6.5 的正确语法


我在这篇文章中找到了答案:RxJS 6: What's new and what has changed? (来自官方文档):

解决方案是转换:

import { combineLatest } from 'rxjs/operators';

a$.pipe(combineLatest(b$, c$));

进入:

import { combineLatest } from 'rxjs';

combineLatest([a$, b$, c$]);
于 2018-05-10T15:24:37.133 回答
3

rxjs 版本 6.4.0

您应该从 RxJs 运算符中导入 map 运算符以使其工作

combineLatest(a$, b$, c$).pipe(map([a, b, c]) => treat(a, b, c))

于 2019-04-03T10:43:49.580 回答
0

combineLatest 与订阅者列表:

combineLatest([aSubscriber, bSubscriber]).pipe(map((aRes, bRes)=>{
// your business logic
}));
于 2020-11-25T07:20:41.610 回答