首页 > 解决方案 > Concat给出警告并且不会执行

问题描述

以下方法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]
    }
  })
}

作为参考,这里是“统一搜索”界面:

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版本不起作用?谢!

标签: angularrxjs

解决方案


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]

您可能希望查看 usingcombineLatest来组合来自每个源的发射,并在每次源 observable 之一发射时发射组合的最新值。这和之间的区别在于forkJoincombineLatest每次源 observables 发出时都会发出,而forkJoin只有在所有源 observables 完成后才发出。


推荐阅读