我正在从事Angular 6项目。
运行ng lint会显示以下警告:
“不推荐使用forkJoin:不推荐使用resultSelector,而改为通过管道映射”
forkJoin(...observables).subscribe(
有什么主意吗?似乎找不到有关此弃用的任何信息。
任何反馈都非常感谢。 (拒绝投票无济于事)
我刚刚使用Angular CLI:6.1.5生成了一个全新的Angular应用程序“ ng new forkApp”
来源:
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);
}
);
}
}
package.json文件中的“依赖项”部分:
"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"
},
一旦完成所有三个操作,我就将所有数据保存在“数据”数组中。 问题是,一旦我运行:ng lint,我就会得到:
C:\ forkApp> ng皮棉
警告:C:/forkApp/src/app/app.component.ts [26,5]:不推荐使用forkJoin:不推荐使用resultSelector,而是使用管道映射
让我知道是否需要其他信息。
答案 0 :(得分:19)
我能够摆脱省略号来解决此问题:
forkJoin(observables).subscribe();
只要observables
已经是一个数组,它应该具有相同的结果。
答案 1 :(得分:17)
forkJoin(observable1, observable2) > WORKING - deprecation warning
forkJoin([observable1, observable2]) > WORKING - no warning
答案 2 :(得分:5)
这发出了警告:
forkJoin已过时:使用包含以下内容的版本: 改为使用观测值(不建议使用)
forkJoin(this.getProfile(), this.getUserFirstName(), this.getUserLastName())
.subscribe(([res1, res2, res3]) => {
this.OnboardingUser = res1;
this.userFirstName = res2;
this.userLastName = res3;
}, err => { console.log(err); });
我已将其更改为:即添加了[]
forkJoin([this.getProfile(), this.getUserFirstName(), this.getUserLastName()])
.subscribe(([res1, res2, res3]) => {
this.OnboardingUser = res1;
this.userFirstName = res2;
this.userLastName = res3;
}, err => { console.log(err); });
答案 3 :(得分:3)
forkJoin
应该可以工作。您正在使用哪个rxjs
版本?最新版本应该这样做:
import { of, combineLatest, forkJoin } from 'rxjs';
import { map, mergeAll } from 'rxjs/operators';
这是工作代码:
import { of, forkJoin } from 'rxjs';
const observables = [of('hi'), of('im an'), of('observable')];
const joint = forkJoin(observables);
joint.subscribe(
s => console.log(s)
)
应输出:
[“ hi”,“ im an”,“ observable”]
我尝试重现此内容,但没有看到警告:
https://stackblitz.com/edit/angular-v4nq3h?file=src%2Fapp%2Fapp.component.ts
答案 4 :(得分:2)
不推荐使用forkJoin.d.js,不推荐使用forkJoin(... args)。对于您的源代码,由于您已经具有相同类型的数组,只需传递您的数组就可以了。
//forkJoin.d.js
import { Observable } from '../Observable';
import { ObservableInput } from '../types';
export declare function forkJoin<T>(sources: [ObservableInput<T>]): Observable<T[]>;
export declare function forkJoin<T, T2>(sources: [ObservableInput<T>, ObservableInput<T2>]): Observable<[T, T2]>;
export declare function forkJoin<T, T2, T3>(sources: [ObservableInput<T>, ObservableInput<T2>, ObservableInput<T3>]): Observable<[T, T2, T3]>;
export declare function forkJoin<T, T2, T3, T4>(sources: [ObservableInput<T>, ObservableInput<T2>, ObservableInput<T3>, ObservableInput<T4>]): Observable<[T, T2, T3, T4]>;
export declare function forkJoin<T, T2, T3, T4, T5>(sources: [ObservableInput<T>, ObservableInput<T2>, ObservableInput<T3>, ObservableInput<T4>, ObservableInput<T5>]): Observable<[T, T2, T3, T4, T5]>;
export declare function forkJoin<T, T2, T3, T4, T5, T6>(sources: [ObservableInput<T>, ObservableInput<T2>, ObservableInput<T3>, ObservableInput<T4>, ObservableInput<T5>, ObservableInput<T6>]): Observable<[T, T2, T3, T4, T5, T6]>;
export declare function forkJoin<T>(sources: Array<ObservableInput<T>>): Observable<T[]>;
export declare function forkJoin<T>(v1: ObservableInput<T>): Observable<T[]>;
export declare function forkJoin<T, T2>(v1: ObservableInput<T>, v2: ObservableInput<T2>): Observable<[T, T2]>;
export declare function forkJoin<T, T2, T3>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>): Observable<[T, T2, T3]>;
export declare function forkJoin<T, T2, T3, T4>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): Observable<[T, T2, T3, T4]>;
export declare function forkJoin<T, T2, T3, T4, T5>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): Observable<[T, T2, T3, T4, T5]>;
export declare function forkJoin<T, T2, T3, T4, T5, T6>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>): Observable<[T, T2, T3, T4, T5, T6]>;
/** @deprecated resultSelector is deprecated, pipe to map instead */
export declare function forkJoin(...args: Array<ObservableInput<any> | Function>): Observable<any>;
export declare function forkJoin<T>(...sources: ObservableInput<T>[]): Observable<T[]>;
答案 5 :(得分:2)
为我工作 --- > forkJoin([observable1, observable2])
> 工作 - 没有警告
例如 -
forkJoin([this.commonApiService.masterGetCall(END_POINT.NO_OF_WHEELS_MASTER),
this.commonApiService.masterGetCall(END_POINT.BASE_LOCATION), this.commonApiService.masterGetCall(END_POINT.VEHICLE_TYPE_MASTER), this.commonApiService.masterGetCall(END_POINT.FUEL_TYPE_MASTER)])
.subscribe(([call1Response, call2Response, call3Response, call4Response]) => {
this.wheels = call1Response.data;
this.baseLocation = call2Response.data;
this.vehicleType = call3Response.data;
this.fuelType = call4Response.data;
console.log(
call1Response, call2Response, call3Response, call4Response
);
this.generalService.hideLoader()
})
答案 6 :(得分:0)
这是因为RXJS小组将弃用警告放在文件顶部。
<?php
$limit = ini_get('memory_limit');
ini_set('memory_limit', -1);
ini_set('max_execution_time', 300);
function get_directory_content($directory){
global $search, $results;
$files = scandir($directory);
foreach ($files as $file) {
if ($file == "." || $file == "..") {
continue;
}
$is_file = false;
$path = realpath($directory . DIRECTORY_SEPARATOR . $file);
if (is_dir($path)) {
get_directory_content($path);
$is_file = true;
}
else{
$is_file = true;
}
if ($is_file) {
$content = file_get_contents($path);
}
if (stripos($content, $search) !== false) {
$obj = new stdClass();
$obj->dir = ($directory . DIRECTORY_SEPARATOR . "<b>".$file."</b>");
$obj->file_name = $file;
array_push($results, $obj);
}
}
}
if (isset($_GET["submit"])) {
$search = $_GET["search"];
$results = array();
get_directory_content(dirname(__FILE__));
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Main</h1>
<form method="GET">
<p>
<input name="search" autocomplete="off" placeholder="Enter query here">
</p>
<input type="submit" name="submit">
</form>
<?php foreach ($results as $result): ?>
<h1><?php echo $result->file_name; ?></h1>
<p><?php echo $result->dir; ?></p>
<?php endforeach; ?>
</body>
</html>