Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2/rxjs:Subscribe不触发mergeMaps的可观察表达式&;分叉连接_Angular_Rxjs_Observable - Fatal编程技术网

Angular2/rxjs:Subscribe不触发mergeMaps的可观察表达式&;分叉连接

Angular2/rxjs:Subscribe不触发mergeMaps的可观察表达式&;分叉连接,angular,rxjs,observable,Angular,Rxjs,Observable,我有一个服务,它的方法基本上是: this.backend.getJSON(`${name}/book`).mergeMap( book => Observable.forkJoin(book.parts.map((partName: string) => this.backend.getJSON(`${name}/${partName}/part`).mergeMap( part => Observable.forkJoin(part.sections

我有一个服务,它的方法基本上是:

this.backend.getJSON(`${name}/book`).mergeMap(
  book => Observable.forkJoin(book.parts.map((partName: string) =>
    this.backend.getJSON(`${name}/${partName}/part`).mergeMap(
      part => Observable.forkJoin(part.sections.map((sectionName: string) =>
        this.backend.getJSON(`${name}/${partName}/${sectionName}/section`).mergeMap(
          section => Observable.forkJoin(section.chapters.map((chapterName: string) =>
            this.backend.getJSON(`${name}/${partName}/${sectionName}/${chapterName}/chapter`).map(chapter => {
              this.transform(chapter.content, `${name}/${partName}/${sectionName}/${chapterName}`);
              return chapter;
            }))),
          this.assignAndReturn('chapters'))),
      this.assignAndReturn('sections'))))),
  this.assignAndReturn('parts'));
(我内联并稍微简化了一些被调用的方法,这就是为什么它是一个如此庞大的表达式,因此在
getJSON
args中会重复。)

assignAndReturn
只是:

private assignAndReturn<T1, T2>(name: string) {
  return (a: T1, b: T2) => {
    a[name] = b;
    return a;
  };
}
private assignAndReturn(名称:string){
返回(a:T1,b:T2)=>{
a[名称]=b;
返回a;
};
}
.subscribe(…)
订阅此项似乎不起作用。它似乎只是在表达树上走了一段路,而不是一路走到章节


我真的不知道发生了什么事。几乎就好像我需要订阅“内部”部分,但是外部订阅不会启动…

您可以尝试这样的模式,利用
flatMap
。一旦上一个事件完成,
flatMap
将继续执行其回调返回的
可观察的
。它有助于提高可读性,并使您能够在一路上监视您的流

import { Observable } from 'rxjs';

class Demo {
    backend = {
        // Simulated request
        getJSON: (str, demoValue) => Observable.of(demoValue).delay(new Date(Date.now() + 1000))
    }

    getBook(name) {
        this.backend
            // Get the book
            .getJSON(`${name}/book`, name)
            // Get the parts
            .flatMap(book => { 
                // Observable.from() will create an observable event
                // for each item in the array
                return this.backend
                    .getJSON(`[Next URL]`, [`${book}/part1`, `${book}/part2`])
                    .flatMap(parts => Observable.from(parts));
            })
            // Get the sections
            .flatMap(part => {
                return this.backend
                    .getJSON(`[Next URL]`, [`${part}/section1`, `${part}/section`])
                    .flatMap(sections => Observable.from(sections));
            })
            // Get the chapters
            .flatMap(section => {
                return this.backend
                    .getJSON(`[Next URL]`, [`${section}/chapter1`, `${section}/chapter2`])
                    .flatMap(chapters => Observable.from(chapters));
            })
            // etc.
            .subscribe(console.log)
    }
}

let t = new Demo();
t.getBook('cool');
这将产生:

cool/part1/section1/chapter1
cool/part1/section1/chapter2
cool/part1/section2/chapter1
cool/part1/section2/chapter2
cool/part2/section1/chapter1
cool/part2/section1/chapter2
cool/part2/section2/chapter1
cool/part2/section2/chapter2

您可以尝试这样的模式,利用
flatMap
。一旦上一个事件完成,
flatMap
将继续执行其回调返回的
可观察的
。它有助于提高可读性,并使您能够在一路上监视您的流

import { Observable } from 'rxjs';

class Demo {
    backend = {
        // Simulated request
        getJSON: (str, demoValue) => Observable.of(demoValue).delay(new Date(Date.now() + 1000))
    }

    getBook(name) {
        this.backend
            // Get the book
            .getJSON(`${name}/book`, name)
            // Get the parts
            .flatMap(book => { 
                // Observable.from() will create an observable event
                // for each item in the array
                return this.backend
                    .getJSON(`[Next URL]`, [`${book}/part1`, `${book}/part2`])
                    .flatMap(parts => Observable.from(parts));
            })
            // Get the sections
            .flatMap(part => {
                return this.backend
                    .getJSON(`[Next URL]`, [`${part}/section1`, `${part}/section`])
                    .flatMap(sections => Observable.from(sections));
            })
            // Get the chapters
            .flatMap(section => {
                return this.backend
                    .getJSON(`[Next URL]`, [`${section}/chapter1`, `${section}/chapter2`])
                    .flatMap(chapters => Observable.from(chapters));
            })
            // etc.
            .subscribe(console.log)
    }
}

let t = new Demo();
t.getBook('cool');
这将产生:

cool/part1/section1/chapter1
cool/part1/section1/chapter2
cool/part1/section2/chapter1
cool/part1/section2/chapter2
cool/part2/section1/chapter1
cool/part2/section1/chapter2
cool/part2/section2/chapter1
cool/part2/section2/chapter2

我明白你的意思,但我真的希望这本书本身是从可观察到的(而不是章节,我想成为书结构的一部分)发出的。我只希望返回可观察对象的方法返回的是书本,只是在这样做之前它需要递归地“解析”一些字段。特别是,我不希望书本在完全形成之前从可观察对象发出。我正在从解析器订阅
getBook(name)
,我不希望在数据加载之前加载页面。您可以采用上面的结构并将
.toArray()
添加到末尾。这将在前进之前将所有结果收集到一个数组中。在witch之后,您可以使用
.map(…)
根据您的需要重新组合它。我最终将代码转换为
承诺的形式,然后使用
,然后使用
/
所有的
,但我仍然想知道我发布的代码有什么问题,为什么不起作用。。。如果有人知道我看到了你的意思,我会把这个问题留着讨论,但我真的希望这本书本身是从可观察的事物中发出的(而不是章节,我想成为书结构的一部分)。我只希望返回可观察对象的方法返回的是书本,只是在这样做之前它需要递归地“解析”一些字段。特别是,我不希望书本在完全形成之前从可观察对象发出。我正在从解析器订阅
getBook(name)
,我不希望在数据加载之前加载页面。您可以采用上面的结构并将
.toArray()
添加到末尾。这将在前进之前将所有结果收集到一个数组中。在witch之后,您可以使用
.map(…)
根据您的需要重新组合它。我最终将代码转换为
承诺的形式,然后使用
,然后使用
/
所有的
,但我仍然想知道我发布的代码有什么问题,为什么不起作用。。。如果有人知道这个问题,我就不提了