Darien Fawkes Darien Fawkes - 3 months ago 218
AngularJS Question

How import toPromise into component?

I use Angular 2.0 in ASP.NET project and stuck with error of importing rxjs into component to use in

this.http.get
/
this.http.post
.

Angular settings:

"@angular/common": "2.0.0-rc.4",
"@angular/compiler": "2.0.0-rc.4",
"@angular/core": "2.0.0-rc.4",
"@angular/forms": "0.2.0",
"@angular/http": "2.0.0-rc.4",
"@angular/platform-browser": "2.0.0-rc.4",
"@angular/platform-browser-dynamic": "2.0.0-rc.4",
"@angular/router": "3.0.0-beta.2",
"@angular/router-deprecated": "2.0.0-rc.2",
"@angular/upgrade": "2.0.0-rc.4",

"rxjs": "5.0.0-beta.6"


On component:

import 'rxjs/add/operator/toPromise';


In function I try to get data from
http.get
requet and handle responce with
toPromise
:

getData(): Promise<IToDo[]> {
return this.http.get('app/shared/todos.json')
.toPromise() // <- ERROR
.then(res => res.json())
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
return Promise.reject(error.message || error);
}


I guess
toPromise()
not imported correct. But I check in
node_modules
all forders exist and
toPromise.d.ts
,
toPromise.js
,
toPromise.js.map
available.

ERROR:

Severity Code Description Project File Line Suppression State
Error TS2339 Property 'toPromise' does not exist on type 'Observable<Response>'. TypeScript Virtual Projects C:\Users\max\Desktop\LocalAccountsApp-master\LocalAccountsApp\app\home\GL\gl.service.ts 39 Active

Answer

This is generally caused by the latest versions. The solution to this will be using RC-4 or RC-5 release along with RX-JS. You can have a generic import 'rxjs/Rx';

Comments