【问题标题】:Angular 6 - ForkJoin has no exported memberAngular 6 - ForkJoin 没有导出的成员
【发布时间】:2018-10-16 01:45:08
【问题描述】:

我已经更新到 Angular 6,我正在尝试使用 ForkJoin,所以在我的服务上我有:

import { forkJoin } from 'rxjs/observable/forkJoin';

但它不承认是,我得到:

...ForkJoin has no exported member

我该如何解决这个问题?

【问题讨论】:

  • 尝试导入'rxjs/add/observable/forkJoin'
  • 我在下面为 angular v6 和 rxjs v6 添加了答案。希望这会帮助这个问题。 @FatehMohamed 检查答案。导入的方式发生了很大变化
  • 哦,好的,谢谢,我还没有更新到第六版

标签: angular typescript rxjs


【解决方案1】:

service.ts:

   import { HttpClient } from '@angular/common/http';
   import {forkJoin} from 'rxjs';

   constructor(private http: HttpClient) { }
   getDetails(){
    let url1 ="../assets/sample.json";
    let url2 ="../assets/sampledata.json";
    //return this.http.get(url);
    return forkJoin(this.http.get(url1),
     this.http.get(url2));  
   }

component.ts:

import { Component,OnInit } from '@angular/core';
import { CommonServiceService } from './common-service.service';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  second: any;
  title = 'app';
  mydata:any;
  constructor(private service: CommonServiceService) {} 
  ngOnInit(){

    this.service.getDetails().subscribe(data=>{
      this.first= data[0];
      console.log(this.mydata);
      this.second = data[1];
      console.log(this.second);
    })
  }
}

【讨论】:

    【解决方案2】:

    将您的导入更改为 -

    import {forkJoin} from 'rxjs';
    

    Rxjs 6,连同更简单的导入路径,现在具有更易于使用的运算符方法。您现在不必使用 Observable 补丁。

    所以不是:

    return Observable.forkJoin( this.http.get('jsonplaceholder.typicode.com/posts'), this.http.get('jsonplaceholder.typicode.com/users') ); 
    

    你可以简单地使用:

    return forkJoin(this.http.get('jsonplaceholder.typicode.com/posts'), this.http.get('jsonplaceholder.typicode.com/users'));
    

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      RxJS 6 具有新的更简单的导入路径,并且摆脱了可链接的操作符,转而支持可管道的操作符。这使得库作为一个整体更加可摇树,并且会产生更小的包。

      如下更改您的导入,它应该可以工作

      import { forkJoin } from 'rxjs';
      

      升级到 Angular 时关于 rxjs 的更多示例

      // creation and utility methods
      import { Observable, Subject, pipe } from 'rxjs';
      // operators all come from `rxjs/operators`
      import { map, takeUntil, tap } from 'rxjs/operators';
      

      答案来源-Upgrading and Summary of New Features

      【讨论】:

      • 我试过了,但 Observable 不知道 forkJoin? -retrieveSeveral() { return Observable.forkJoin( this.http.get('jsonplaceholder.typicode.com/posts'), this.http.get('jsonplaceholder.typicode.com/users') ); }
      • 我看到答案被接受了。可以解决评论中提到的问题吗? @Craig2018
      • 好吧,您回答了我的问题,尽管我的代码由于其他原因无法正常工作:retrieveSeveral() { return Observable.forkJoin( this.http.get('jsonplaceholder.typicode.com/posts') , this.http.get('jsonplaceholder.typicode.com/users') ); }
      猜你喜欢
      • 2018-11-25
      • 2019-04-13
      • 2017-07-07
      • 2018-09-25
      • 2018-05-22
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多