[英]Angular 6 ng lint combineLatest is deprecated
I recently updated from Angular 5 to Angular 6.我最近从 Angular 5 更新到了 Angular 6。
I'm getting this warning combineLatest is deprecated: resultSelector no longer supported, pipe to map instead
.我收到此警告
combineLatest is deprecated: resultSelector no longer supported, pipe to map instead
。 Rxjs is version 6.1.0, tslint is 5.10.0, Angular CLI is 6.0.0 and Typescript 2.7.2. Rxjs 是 6.1.0 版本,tslint 是 5.10.0,Angular CLI 是 6.0.0 和 Typescript 2.7.2。 I'm using it like this:
我是这样使用它的:
const a$ = combineLatest(
this.aStore.select(b.getAuth),
this.cStore.select(b.getUrl),
(auth, url) => ({auth, url}),
);
I've tried it also like this:我也这样试过:
empty().pipe(
combineLatest(...),
...
)
But this gives me: combineLatest is deprecated: Deprecated in favor of static combineLatest
and empty is also deprecated in favor of its static version.但这给了我:
combineLatest is deprecated: Deprecated in favor of static combineLatest
和 empty 也已弃用,以支持其静态版本。
combineLatest is deprecated: resultSelector no longer supported, pipe to map instead
不推荐使用 combineLatest:不再支持 resultSelector,改为使用管道映射
The above warning is recommending to remove the resultSelector the last function you provided in combineLatest observable and provide it as part of map operator as follows上述警告建议删除您在 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]}))
)
UPDATE:更新:
If you see combineLatest is deprecated: Pass arguments in a single array instead
then just add []:如果您看到
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]}))
)
Unfortunately you might also get that tslint error if you import combineLatest from operators:不幸的是,如果您从运算符导入 combineLatest ,您也可能会遇到该 tslint 错误:
import { combineLatest } from 'rxjs/operators';
combineLatest(...array);
instead of,代替,
import { combineLatest } from 'rxjs';
combineLatest(...array);
Unlike the deprecated versions, combineLatest
accepts an Array of Observable
and returns an array containing the latest values from each.不同于弃用版本,
combineLatest
接受的数组Observable
并返回包含从每个最新值的数组。 Every stream has to yield in order for combineLatest
to yield.每个流都必须屈服才能使
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
}
}));
For trailing comma
error, remove the comma after (auth, url) => ({auth, url})
对于
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.
);
For missing import
error, Make sure you have imports for all the external var's or classes you are using in the file.对于
missing import
错误,请确保您在文件中使用的所有外部变量或类都有导入。
Example, in this case, if you havent imported combineLatest
, then import it例如,在这种情况下,如果您
combineLatest
导入combineLatest
,则将其导入
import { combineLatest } from 'rxjs'; // For RxJS 6.x
import { combineLatest } from 'rxjs/operators'; // For RxJS 5.x
In my case it's because I explicitly set generic argument, so an incorrect combineLatest
overload was selected.就我而言,这是因为我明确设置了泛型参数,因此选择了不正确的
combineLatest
重载。 To get rid of the warning I've changed为了摆脱我改变的警告
combineLatest<void>([
firstObservable$,
secondObservable$
]);
to到
combineLatest([
firstObservable$,
secondObservable$
]).pipe(
mapTo(undefined)
);
i used depricated combineLatest to combine those two observables this.route.paramMap + this.route.queryParamMap:我使用废弃的 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');
})
I would solve that this way:我会这样解决:
auth$ = this.aStore.select(b.getAuth);
url$ = this.cStore.select(b.getUrl);
combinedResult$ = combineLatest([this.auth$, this.url$]).pipe(
map(([auth, url]) => ({auth, url}))
)
This error can also be caused by passing in an array of Subscription
items, instead of actual observables, lol.这个错误也可能是由于传入了一组
Subscription
项而不是实际的 observables 引起的,哈哈。 (I got really sloppy on this one.) (我对这个很草率。)
WRONG!错误的!
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
})
CORRECT!正确的!
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.