【发布时间】:2018-01-09 14:57:49
【问题描述】:
我是 Angular 2 的新手,我希望我能解释清楚。我有 3 个 http 路径 - 1、2 和 3。我想合并/覆盖这些。我希望 path3 成为我的默认值。
如果 path2 中存在一个值,我希望该值覆盖 path3 中的该值,并且与 path1 相同。因此,path1 中的值是最重要的,但如果 path1 中的值为 null,则应使用 path2 中的值,然后使用 path3。我试过使用forkJoin 没有成功。
如果 path1 或 path2 中的值不存在,则会显示路径名而不是分配的值。这就是我所拥有的:
export class Loader1 implements Loader2 {
constructor(private http: HttpClient) {
}
public getMethod(lang: string): any {
return Observable.forkjoin(
this.http.get(path1 + '.json').map((res) => res)
.catch((res) => Observable.of(null)),
this.http.get(path2 + '.json').map((res) => res),
this.http.get(path3 + '.json').map((res) => res)
).map(results => {
let emptyCheck = this.http.get(path1 + '.json');
if (emptyCheck.catch((res) => Observable.of(null))) {
results[0] = results[1];
} else {
emptyCheck.map((res) => res);
}
let result = Object.assign({}, results[2], results[1], results[0]);
return result;
});
}
}
path1:
{
"value1": "First number1",
"value5": "First number5",
"value10": "First number10"
}
path2:
{
"value1": "Second number1",
"value2": "Second number2",
"value4": "Second number4",
"value5": "Second number5",
"value6": "Second number6",
"value7": "Second number7",
"value10": "Second number10"
}
path3:
{
"value1": "Third number1",
"value2": "Third number2",
"value3": "Third number3",
"value4": "Third number4",
"value5": "Third number5",
"value6": "Third number6",
"value7": "Third number7",
"value8": "Third number8",
"value9": "Third number9",
"value10": "Third number10"
}
Expected Output:
First number1
Second number2
Third number3
Second number4
First number5
Second number6
Second number7
Third number8
Third number9
First number10
Actual output:
path1.value1
path2.value2
Third number3
path2.value4
path1.value5
path2.value6
path2.value7
Third number8
Third number9
path1.value10
【问题讨论】:
-
你试过.merge吗?
-
forkjoin将运行所有三个 http 请求,只有当所有三个请求都完成后,它才会执行subscribe函数。但是,在您的情况下,您似乎希望以某种顺序执行调用。flatMap或许能帮到你。见stackoverflow.com/questions/34523338/…
标签: angular http rxjs observable fork-join