【问题标题】:Observable.forkJoin call delayed after return of function函数返回后 Observable.forkJoin 调用延迟
【发布时间】:2017-01-19 18:29:14
【问题描述】:

在组件中,我们订阅了来自服务的两个 http 返回结果,并且在服务中,两个 http 调用返回了可观察的。

在组件打字稿中

catalogPromise = this.ondemandService.getVodCatalog().subscribe((data) => {
        console.log(data);

        });

服务中:-

    getVodCatalog(url,user){
    return this.getVodCategories().map((data) => {
            let result,result2;
            let Observers = [],
            cats = [],
            catalogs = [];

            result=data.json();
            result.categorylist.forEach((catergory) => {
                cats.push(catergory.name);
                Observers.push(this.getCategoriesCatolog(catergory.id,{}));
            });
            console.log(Observers);
            Observable.forkJoin(Observers).subscribe((vodCatalogs)=> {
                vodCatalogs.forEach((vodeCatalog,idx)=>{
                    let v1 = vodeCatalog.json();
                    if(v1.vodlist)
                    {
                        catalogs.push({
                            label: cats[idx],
                            list: v1.vodlist
                            });
                    }
                });
                return catalogs;
            });

        })
    .catch(this.handleError);
}


    getVodCategories(){
        let url="http://CategoryList";
        let headers = new Headers({ 'Content-Type':     'application/json','Accept':'application/json, text/plain, */*'});
        let body = {
                offset: 0,
                count: -1,
                type: 'dfasd',
                categoryid: -1,
             };
        let options = new RequestOptions({
             headers: headers,
             url:url,
             withCredentials: true,
             timeout:30000,
             body:body
         });
         return this._http.post(url,body,options);
     }

     getCategoriesCatolog(cateogoryId,options){
         var url="http://VodList";
         let headers = new Headers({ 'Content-Type':    'application/json','Accept':'application/json, text/plain, */*' });
         let body = {
                  count: 100,
                  offset: 0,
                  orderType: 0,
                  categoryid: cateogoryId,
            };
            options = new RequestOptions({
             url:url,
             withCredentials: true,
             timeout:30000,
             body:body
         });
        return this._http.post(url,body,options);
    }

在服务中,getVodCategories() 和 getCategoriesCatolog() 返回 observable 结果,但是当我们尝试在组件中订阅它时,整个数据未定义,之后 Observable.forkjoin() 正在运行。

【问题讨论】:

    标签: angularjs angular typescript ecmascript-6 rxjs


    【解决方案1】:

    尽量避免嵌套的 observable ,像下面这样使用 merge insteda

    Rx.Observable.merge(
      myService.upload('upload1'),
      myService.upload('upload2')
    .take(2)
    .subscribe({complete: () => { ... });
    

    【讨论】:

    • 问题是 result.categorylist.forEach((catergory) => { cats.push(catergory.name); Observers.push(this.getCategoriesCatolog(catergory.id,{})); } );
    • 问题是 result.categorylist.forEach((catergory) => { cats.push(catergory.name); Observers.push(this.getCategoriesCatolog(catergory.id,{})); } );当我们调用 getCategoriesCatalog 时,它会删除来自 catelogylist 的 cat id 参数,Observable 数组来,并且 Observables 包含我们必须在组件数据中返回的不同响应。但是使用合并后我无法订阅所需的结果。第一个调用结果应该用在第二个调用参数中。这是问题请回复
    • 从服务中返回承诺并在组件中捕获它。在服务中,您可以订阅所有内容。
    猜你喜欢
    • 2011-09-14
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    • 2017-09-06
    相关资源
    最近更新 更多