如果 angular 5 中的 forKJoin 参数为 null,如何避免调用 api?
How to avoid api call if argument is null in forKJoin in angular 5?
this.hobbies :any[]=[];
this.worklist : any[]=[];
forkJoin(this.personService.addHobbies(this.hobbies),this.professionService.updateWorks(this.worklist))
.subscribe(res => {});
这里我用 forkJoin 调用 2 apis this.personService.addHobbies(this.hobbies) 和 this.professionService.updateWorks(this.worklist),但我想避免如果 this.hobbies 是空列表,api 调用 this.personService.addHobbies().. 如果 this.worklist 不是空列表,只有 this.professionService.updateWorks(this.worklist) 应该调用。
如果列表为空或为空,是否有任何解决方案可以避免特定的 api 调用。请帮助
您可以使用一个数组,在其中添加您的 API 调用:
this.hobbies: any[] = [];
this.worklist: any[] = [];
const requests = [];
if (this.hobbies.length > 0){
requests.push(this.personService.addHobbies(this.hobbies));
}
if (this.worklist.length > 0) {
requests.push(this.professionService.updateWorks(this.worklist));
}
forkJoin(requests).subscribe(res => {});
this.hobbies :any[]=[];
this.worklist : any[]=[];
forkJoin(this.personService.addHobbies(this.hobbies),this.professionService.updateWorks(this.worklist))
.subscribe(res => {});
这里我用 forkJoin 调用 2 apis this.personService.addHobbies(this.hobbies) 和 this.professionService.updateWorks(this.worklist),但我想避免如果 this.hobbies 是空列表,api 调用 this.personService.addHobbies().. 如果 this.worklist 不是空列表,只有 this.professionService.updateWorks(this.worklist) 应该调用。 如果列表为空或为空,是否有任何解决方案可以避免特定的 api 调用。请帮助
您可以使用一个数组,在其中添加您的 API 调用:
this.hobbies: any[] = [];
this.worklist: any[] = [];
const requests = [];
if (this.hobbies.length > 0){
requests.push(this.personService.addHobbies(this.hobbies));
}
if (this.worklist.length > 0) {
requests.push(this.professionService.updateWorks(this.worklist));
}
forkJoin(requests).subscribe(res => {});