typescript Angular 6 - ForkJoin 没有导出成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50194098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 6 - ForkJoin has no exported member
提问by Suvendu
I have updated to Angular 6 and I'm trying to use ForkJoin, so on my service I have:
我已经更新到 Angular 6 并且我正在尝试使用 ForkJoin,所以在我的服务中我有:
import { forkJoin } from 'rxjs/observable/forkJoin';
But it's not recognising is and I'm getting:
但它没有认识到是,我得到:
...ForkJoin has no exported member
How can I fix this?
我怎样才能解决这个问题?
回答by Suvendu
Change your import to -
将您的导入更改为 -
import {forkJoin} from 'rxjs';
Rxjs 6, along with simpler import paths, have now more easy to use operator methods. You dont have to use the Observable patch now.
Rxjs 6 以及更简单的导入路径,现在拥有更易于使用的操作符方法。您现在不必使用 Observable 补丁。
So Instead of :
所以而不是:
return Observable.forkJoin( this.http.get('jsonplaceholder.typicode.com/posts'), this.http.get('jsonplaceholder.typicode.com/users') );
You can simply use :
您可以简单地使用:
return forkJoin(this.http.get('jsonplaceholder.typicode.com/posts'), this.http.get('jsonplaceholder.typicode.com/users'));
Hope this helps!
希望这可以帮助!
回答by Saiyaff Farouk
RxJS 6 has new and simpler import paths and gets away with chainable operators in favor of pipeable operators. This makes the library as a whole more tree-shakable and will result in smaller bundles.
RxJS 6 具有新的更简单的导入路径,并摆脱了可链接的运算符,而支持可管道的运算符。这使得整个库更易于摇摇晃晃,并会产生更小的包。
Change your import as below and it should work
如下更改您的导入,它应该可以工作
import { forkJoin } from 'rxjs';
Few more examples on rxjs when upgrading to angular
升级到 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';
Answer source - Upgrading and Summary of New Features
答案来源——新功能的升级与总结
回答by Rajesh Valluru
service.ts:
服务.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:
组件.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);
})
}
}