angular 2 Observable 完成未调用

angular 2 Observable complete not called

我正在玩 hero app 从 angular 2 教程开始,现在我有这个组件

import { Component, OnInit } from '@angular/core'
import { Subject } from 'rxjs/Subject';
import { Hero } from "./hero";
import { Router } from "@angular/router";
import { HeroService } from "./hero.service";
import { BehaviorSubject } from "rxjs/BehaviorSubject";


@Component({
    selector: 'hero-search',
    templateUrl: 'app/hero-search.component.html',
    styleUrls: ['app/hero-search.component.css'],
})
export class HeroSearchComponent implements OnInit{
    heroes: Hero[];
    isLoading: BehaviorSubject<boolean> = new BehaviorSubject(false);
    error: any;
    private searchNameStream = new Subject<string>();

    constructor(
        private heroService: HeroService,
        private router: Router
    ) {}

    ngOnInit() {
        this.searchNameStream
            .debounceTime(400)
            .distinctUntilChanged()
            .switchMap(name => {
                this.isLoading.next(true);
                return this.heroService.getHeroesByName(name)
            })
            .subscribe(
                heroes => this.heroes = heroes,
                error => this.error = error,
                () => {
                    console.log('completed');
                    this.isLoading.next(false);
                })
    }

    // Push a search term into the observable stream.
    search(Name: string): void {
        this.searchNameStream.next(Name)
    }

    gotoDetail(hero: Hero): void {
        let link = ['/detail', hero.id];
        this.router.navigate(link);
    }

}

问题是,如果我理解正确的话,subscribe 需要三个回调参数.subscribe(success, failure, complete);。但就我而言,完整的部分从未执行过。我想这与 switchMap 的工作方式有关。我说的对吗?

searchNameStream 永远不会完成,因此从 switchMap 获得的 Observable 也永远不会完成。

每次 searchNameStream 发出一个事件时,heroService.getHeroesByName() 就会被调用,并且 "inner" 可观察量发出的所有事件都会被 "outer" 可观察量重新发出,直到 searchNameStream 发出新事件,然后重复此过程。您订阅了外部可观察对象,它只会在 searchNameStream 完成时完成。