以下方法,forkJoin
工作正常,但是。 。 。
unifiedSearch : Function = (query: string) : Observable<UnifiedSearch> => {
return forkJoin(this.searchService.gitSearch(query), this.codeSearchService.codeSearch(query))
.map( (response : [GitSearch, GitCodeSearch]) => {
return {
'repositories' : response[0],
'code': response[1]
}
})
}
。 。 。我试图将它转换为使用concat
作为赋值的一部分,但是当它编译时,我得到一堆警告,并且在浏览器中没有任何渲染。
unifiedSearch : Function = (query: string) : Observable<UnifiedSearch> => {
return concat(this.searchService.gitSearch(query), this.codeSearchService.codeSearch(query))
.map( (response) => {
return {
'repositories' : response[0],
'code': response[1]
}
})
}
作为参考,这里是'UnifiedSearch'接口:
import {GitSearch} from './git-search';
import { GitCodeSearch } from './git-code-search';
export interface UnifiedSearch {
repositories: GitSearch,
code: GitCodeSearch
}
如果有帮助,以下是我收到的警告:
./node_modules/rxjs/Observable/of.js
There are multiple modules with names that only differ in casing.
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
Use equal casing. Compare these module identifiers:
* C:\Users\Johnathan\advanced_angular\node_modules\rxjs\Observable\of.js
Used by 1 module(s), i. e.
C:\Users\Johnathan\advanced_angular\node_modules\rxjs\Observable\concat.js
* C:\Users\Johnathan\advanced_angular\node_modules\rxjs\observable\of.js
Used by 2 module(s), i. e.
C:\Users\Johnathan\advanced_angular\node_modules\@angular\common\@angular\common\http.es5.js
任何想法为什么concat
版本不起作用? TKS!
concat
和forkJoin
的工作方式略有不同。
concat
按照发射的顺序发出每个来源的每个值,并作为concat
算子的参数给出。一旦源完成,它将移动到源阵列中的下一个源。
forkJoin
将给出每个observable的最后发出的值,然后在数组中返回这些值。它将一直等到所有给定的可观察量在它发出之前完成。
以下是一个例子:
const source1 = of(1, 2);
const source2 = of(3, 4);
concat(source1, source2).subscribe(v => console.log(v))
// output (each line is new emit)
// 1
// 2
// 3
// 4
forkJoin(source1, source2).subscribe(v => console.log(v))
// output (each line is new emit)
// [2, 4]
您可能希望使用combineLatest
来组合每个源的发射,并在每次源可观察源发出时发出组合的最新值。这与forkJoin
之间的区别在于combineLatest
每次发出源可观察量时都会发出,而forkJoin
只在所有源可观察源完成后才会发出。