繁体   English   中英

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:不再支持 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]}))
)

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

import { combineLatest } from 'rxjs/operators';

combineLatest(...array);

代替,

import { combineLatest } from 'rxjs';

combineLatest(...array);

不同于弃用版本, combineLatest接受的数组Observable并返回包含从每个最新值的数组。 每个流都必须屈服才能使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
    }
}));

对于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导入combineLatest ,则将其导入

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

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

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

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

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

我使用废弃的 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项而不是实际的 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
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM