Angular 6 ng lint combineLatest 已弃用

Angular 6 ng lint combineLatest is deprecated

我最近从 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 也被弃用,取而代之的是它的静态版本。

combineLatest is deprecated: resultSelector no longer supported, pipe to map instead

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

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]}))
)

对于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

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

import { combineLatest } from 'rxjs/operators';
    
combineLatest(...array);

而不是

import { combineLatest } from 'rxjs';
    
combineLatest(...array);

在我的例子中,这是因为我明确设置了泛型参数,所以选择了不正确的 combineLatest 重载。为了摆脱我已经改变的警告

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

combineLatest([
    firstObservable$,
    secondObservable$
]).pipe(
    mapTo(undefined)
);

与已弃用的版本不同,combineLatest 接受 ObservableA​​rray 和 returns 包含每个最新值的数组。每个流都必须让出 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
    }
}));

我使用已弃用的 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');
  })

我会这样解决:

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

combinedResult$ = combineLatest([this.auth$, this.url$]).pipe(
    map(([auth, url]) => ({auth, url}))
)

此错误也可能是由于传入 Subscription 项数组而不是实际的可观察对象引起的,大声笑。 (我对这个真的很马虎。)

错误!

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
})