我正在开发一个Angular 6项目.
运行ng lint会发出以下警告:
"不推荐使用forkJoin:不推荐使用resultSelector,而是使用管道映射"
forkJoin(...observables).subscribe(
Run Code Online (Sandbox Code Playgroud)
任何的想法?似乎无法找到有关此弃用的任何信息.
任何反馈都非常感谢.(downvoting没有多大帮助)
我刚刚使用Angular CLI生成了一个全新的Angular应用程序"ng new forkApp":6.1.5
资源:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { forkJoin } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'forkApp';
constructor(private http: HttpClient) {}
ngOnInit() {
console.log('ngOnInit...');
const obs = [];
for (let i = 1; i < 4; i++) {
const ob = this.http.get('https://swapi.co/api/people/' + i);
obs.push(ob);
}
forkJoin(...obs)
.subscribe(
datas => {
console.log('received data', datas);
}
);
}
}
Run Code Online (Sandbox Code Playgroud)
package.json文件中的"dependencies"部分:
"dependencies": {
"@angular/animations": "^6.1.0",
"@angular/common": "^6.1.0",
"@angular/compiler": "^6.1.0",
"@angular/core": "^6.1.0",
"@angular/forms": "^6.1.0",
"@angular/http": "^6.1.0",
"@angular/platform-browser": "^6.1.0",
"@angular/platform-browser-dynamic": "^6.1.0",
"@angular/router": "^6.1.0",
"core-js": "^2.5.4",
"rxjs": "^6.0.0",
"zone.js": "~0.8.26"
},
Run Code Online (Sandbox Code Playgroud)
完成所有三次完成后,我获得了"datas"数组中的所有数据.问题是,一旦我运行:ng lint我得到了这个:
C:\ forkApp> ng lint
警告:C:/forkApp/src/app/app.component.ts [26,5]:不推荐使用forkJoin:不推荐使用resultSelector,而是使用管道映射代替
如果您需要任何其他信息,请与我们联系.
rad*_*per 45
forkJoin(observable1, observable2) > WORKING - deprecation warning
forkJoin([observable1, observable2]) > WORKING - no warning
Run Code Online (Sandbox Code Playgroud)
Lan*_*ani 39
我能够通过删除省略号来解决这个问题:
forkJoin(observables).subscribe();
只要observables已经是一个数组,它应该具有相同的结果.
forkJoin应该管用。rxjs您正在使用哪个版本?最新版本应该这样做:
import { of, combineLatest, forkJoin } from 'rxjs';
import { map, mergeAll } from 'rxjs/operators';
Run Code Online (Sandbox Code Playgroud)
这里是工作代码:
import { of, forkJoin } from 'rxjs';
const observables = [of('hi'), of('im an'), of('observable')];
const joint = forkJoin(observables);
joint.subscribe(
s => console.log(s)
)
Run Code Online (Sandbox Code Playgroud)
应该输出:
[“ hi”,“ im an”,“ observable”]
我尝试重现此内容,但没有看到警告:
https://stackblitz.com/edit/angular-v4nq3h?file=src%2Fapp%2Fapp.component.ts
这给出了这个警告:
forkJoin 已弃用:使用采用 Observable 数组的版本代替(弃用)
forkJoin(this.getProfile(), this.getUserFirstName(), this.getUserLastName())
.subscribe(([res1, res2, res3]) => {
this.OnboardingUser = res1;
this.userFirstName = res2;
this.userLastName = res3;
}, err => { console.log(err); });
Run Code Online (Sandbox Code Playgroud)
我已经改变了它:即添加 []
forkJoin([this.getProfile(), this.getUserFirstName(), this.getUserLastName()])
.subscribe(([res1, res2, res3]) => {
this.OnboardingUser = res1;
this.userFirstName = res2;
this.userLastName = res3;
}, err => { console.log(err); });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13830 次 |
| 最近记录: |