Angular 6 - ForkJoin没有导出成员

时间:2018-05-05 21:17:22

标签: angular typescript rxjs

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

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

但它并没有认识到而且我得到了:

...ForkJoin has no exported member

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:37)

将导入更改为 -

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'));

希望这有帮助!

答案 1 :(得分: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

答案 2 :(得分:-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);
    })
  }
}