31

我最近从 Angular 5 更新到 Angular 6。

我收到了这个警告combineLatest is deprecated: resultSelector no longer supported, pipe to map instead。Rxjs 是 6.1.0 版本,tslint 是 5.10.0,Angular CLI 是 6.0.0 和 Typescript 2.7.2。我这样使用它:

const a$ = combineLatest(
  this.aStore.select(b.getAuth),
  this.cStore.select(b.getUrl),
  (auth, url) => ({auth, url}),
);

我也试过这样:

empty().pipe(
combineLatest(...),
  ...
)

但这给了我:combineLatest is deprecated: Deprecated in favor of static combineLatest并且 empty 也被弃用,取而代之的是它的静态版本。

4

8 回答 8

79

combineLatest 已弃用:不再支持 resultSelector,而是使用管道映射

上述警告建议删除您在 combineLatest observable 中提供的最后一个函数 resultSelector 并将其作为 map 运算符的一部分提供,如下所示

const a$ = combineLatest(
  this.aStore.select(b.getAuth),
  this.cStore.select(b.getUrl)
);

const result$ = a$.pipe(
  map(results => ({auth: results[0], url: results[1]}))
)

更新:

如果你看到combineLatest is deprecated: Pass arguments in a single array instead那么只需添加 []:

const a$ = combineLatest([
  this.aStore.select(b.getAuth),
  this.cStore.select(b.getUrl)
]);
    
const result$ = a$.pipe(
  map(results => ({auth: results[0], url: results[1]}))
)
于 2018-05-10T14:06:31.147 回答
11

不幸的是,如果您从运算符导入 combineLatest,您也可能会收到该 tslint 错误:

import { combineLatest } from 'rxjs/operators';

combineLatest(...array);

代替,

import { combineLatest } from 'rxjs';

combineLatest(...array);
于 2018-07-09T12:29:13.780 回答
5

与不推荐使用的版本不同,它combineLatest接受一个ArrayObservable并返回一个包含每个的最新值的数组。每个流都必须屈服才能combineLatest屈服。

fruitType$ = combineLatest([this.entity$, this.datasetStateService.get$('Fruits')])
  .pipe(map(data => {
    const entity = data[0];
    const dataset = data[1];
    return {
       isApple: (dataset.find(ds => ds.label === 'Apple') as DataItem).id === entity.fruitId,
       isOrange: (dataset.find(ds => ds.label === 'Orange') as DataItem).id === entity.fruitId
    }
}));
于 2020-02-07T22:33:19.633 回答
1

如有trailing comma错误,请删除后面的逗号(auth, url) => ({auth, url})

const a$ = combineLatest(
  this.aStore.select(b.getAuth),
  this.cStore.select(b.getUrl),
  (auth, url) => ({auth, url}),  // Remove this comma.
);

对于missing import错误,请确保您对文件中使用的所有外部变量或类都有导入。

例如,在这种情况下,如果您还没有导入combineLatest,则导入它

import { combineLatest } from 'rxjs'; // For RxJS 6.x

import { combineLatest } from 'rxjs/operators'; // For RxJS 5.x
于 2018-05-10T14:10:23.967 回答
1

就我而言,这是因为我明确设置了泛型参数,因此combineLatest选择了不正确的重载。为了摆脱我已经改变的警告

combineLatest<void>([
    firstObservable$,
    secondObservable$
]);

combineLatest([
    firstObservable$,
    secondObservable$
]).pipe(
    mapTo(undefined)
);
于 2019-11-30T08:14:46.410 回答
0

我会这样解决:

auth$ = this.aStore.select(b.getAuth);
url$ = this.cStore.select(b.getUrl);

combinedResult$ = combineLatest([this.auth$, this.url$]).pipe(
    map(([auth, url]) => ({auth, url}))
)
于 2020-08-02T17:05:58.960 回答
0

我使用 deprericated combineLatest 来组合这两个可观察的 this.route.paramMap + this.route.queryParamMap:

combineLatest(this.route.paramMap, this.route.queryParamMap)
  .subscribe(combined => {
    const idFollower = combined[0].get('id');
    const page = combined[1].get('page');
  })
于 2020-04-03T10:29:30.810 回答
0

这个错误也可能是由于传入一个Subscription项目数组而不是实际的 observables 引起的,哈哈。(我对这个真的很草率。)

错误的!

const foo = Foo$.subscribe(f => {
  // do something with f here
})

const bar = Bar$.subscribe(b => {
  // do something with b here
})

combineLatest([foo, bar]).subscribe(([f, b]) => {
  // do something after both foo$ and bar$ have emitted something
})

正确的!

const foo$ = Foo$.pipe(
  tap(f => {
    // do something with f here
  })
)

const bar$ = Bar$.pipe(
  tap(b => {
    // do something with b here
  })
)

combineLatest([foo$, bar$]).subscribe(([f, b]) => {
  // do something after both Foo$ and Bar$ have emitted something
})
于 2020-10-23T22:34:20.840 回答