Angular 6 ng lint combineLatest已弃用

Pha*_*aze 22 rxjs tslint angular angular6

我最近从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}),
);
Run Code Online (Sandbox Code Playgroud)

我也尝试过这样:

empty().pipe(
combineLatest(...),
  ...
)
Run Code Online (Sandbox Code Playgroud)

但是这给了我:combineLatest is deprecated: Deprecated in favor of static combineLatest而且对于静态版本也不推荐使用empty.

Abi*_*das 48

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

上面的警告建议删除resultSelector你在combineLatest observable中提供的最后一个函数,并将其作为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]}))
)
Run Code Online (Sandbox Code Playgroud)

  • 个人偏好,但我更喜欢这里的[解构分配](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)映射到新对象.所以像:`const result $ = a $ .pipe(tap(([auth,url])=> {/*...*/}));` (5认同)

Rui*_*ues 6

不幸的是,如果您从操作员中导入CombineLatest,可能还会收到tslint错误:

import { combineLatest } from 'rxjs/operators';

combineLatest(...array);
Run Code Online (Sandbox Code Playgroud)

代替,

import { combineLatest } from 'rxjs';

combineLatest(...array);
Run Code Online (Sandbox Code Playgroud)


jen*_*ent 5

与已弃用的版本不同,combineLatest它接受Array ofObservable并返回包含每个版本的最新值的数组。每个流都必须屈服才能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
    }
}));
Run Code Online (Sandbox Code Playgroud)