我正在使用Angular 2构建一个MEAN堆栈应用程序。
我知道在StackOverflow上多次询问过这个问题。我一直在研究问题并尝试不同的建议。对于大多数人来说,导入整个rx / js库并映射或导入:
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
似乎解决了这个问题。我一直在尝试不同的组合,但无济于事。我一直在我的服务中的.map的所有实例上得到错误。我尝试更新我的全局打字稿,以及我在依赖项中使用的打字稿版本。在发送'后,我在终端中收到错误。我认为这是一个打字稿问题,但我不确定 - 错误有点模糊,因为导入observable并没有解决它。
{
"name": "aether-news",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/common": "^2.4.0",
"@angular/compiler": "^2.4.0",
"@angular/core": "^2.4.0",
"@angular/forms": "^2.4.0",
"@angular/http": "^2.4.0",
"@angular/platform-browser": "^2.4.0",
"@angular/platform-browser-dynamic": "^2.4.0",
"@angular/router": "^3.4.0",
"@types/googlemaps": "^3.26.8",
"@types/jquery": "^2.0.43",
"@types/lodash": "^4.14.62",
"@types/underscore": "^1.8.0",
"amcharts3-angular2": "github:amcharts/amcharts3-angular2",
"angular2-google-maps": "^0.17.0",
"core-js": "^2.4.1",
"dotenv": "^4.0.0",
"jquery": "^3.2.1",
"lodash": "^4.17.4",
"ng2-google-charts": "^2.1.0",
"rxjs": "^5.1.0",
"tslint": "^4.4.2",
"typescript": "^2.4.2",
"underscore": "^1.8.3",
"zone.js": "^0.7.6"
},
"devDependencies": {
"@angular/cli": "1.0.0-rc.0",
"@angular/compiler-cli": "^2.4.0",
"@types/jasmine": "2.5.38",
"@types/node": "~6.0.60",
"codelyzer": "~2.0.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.0.0",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^0.2.0",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.0",
"ts-node": "~2.0.0",
"tslint": "~4.4.2",
"typescript": "^2.4.2",
"upgrade": "^1.1.0"
}
}
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class NewsApiService {
public bingToken: string;
public bbc: any;
public bingWorld: any;
public bingPolitics: any;
public apNews: any;
public googleNews: any;
public economistNews: any;
public nytNews: any;
public wapoNews: any;
public cnnNews: any;
public newsweekNews: any;
public reutersNews: any;
public guardianUkNews: any;
public guardianAuNews: any;
public huffPostNews: any;
public wsjNews: any;
public eventRegistryBBC: any;
public eventRegistryGuardian: any;
public eventRegistryCNN: any;
public eventRegistryWAPO: any;
public eventRegistryReuters: any;
public eventRegistryNYT: any;
public eventRegistryEconomist: any;
public eventRegistryAP: any;
public eventRegistryWSJ: any;
public eventRegistryNewswire: any;
constructor(
private http: Http,
private router: Router
) {}
//EVENT REGISTRY
//BBC
getEventRegistryBBC(){
return this.http.get("http://eventregistry.org/json/article?sourceUri=bbc.co.uk&sourceUri=bbc.com&categoryUri=dmoz%2FSociety%2FPolitics&action=getArticles&articlesSortBy=date&resultType=articles&articlesCount=200&articlesIncludeArticleDuplicateList=true&articlesIncludeArticleCategories=true&articlesIncludeConceptImage=true&articlesIncludeConceptDescription=true&articlesIncludeConceptDetails=true&articlesIncludeConceptTrendingScore=true&articlesIncludeSourceDescription=true&articlesIncludeArticleImage=true&articlesIncludeSourceDetails=true")
.map((res) => {
this.eventRegistryBBC = res.json()
return res.json().articles.results;
})
}
非常好奇人们是否面临同样的问题,即使正确导入rxjs也是如此。谢谢你的帮助。
答案 0 :(得分:0)
在我的所有服务中,他们都会输入(请参阅Observable)并且他们没有多次退货 - 我不确定如何/如果这样做。
我的服务返回this.http.get - etc - 并使用response.json()映射响应。
在我的组件中,我订阅了Observable并进行任何进一步的数据处理以提取特定数据。
也许尝试这样的事情?
getEventRegistryBBC(): Observable<Response>{
return this.http.get("http://eventregistry.org/json/article?sourceUri=bbc.co.uk&sourceUri=bbc.com&categoryUri=dmoz%2FSociety%2FPolitics&action=getArticles&articlesSortBy=date&resultType=articles&articlesCount=200&articlesIncludeArticleDuplicateList=true&articlesIncludeArticleCategories=true&articlesIncludeConceptImage=true&articlesIncludeConceptDescription=true&articlesIncludeConceptDetails=true&articlesIncludeConceptTrendingScore=true&articlesIncludeSourceDescription=true&articlesIncludeArticleImage=true&articlesIncludeSourceDetails=true")
.map(response => response.json())
}