Angular2 - http.post(...).map 不是函数
Angular2 - http.post(...).map is not a function
我已经查看了所有 github 问题和 Whosebug 帖子,但我无法让它工作
( https://github.com/angular/angular/issues/5632 )
( )
- 我正在使用 Angular2@2.0.0-beta.1
- 当我在控制台中检查 console/resources 时,Rxjs (rxjs@5.0.0-beta.1) 已成功导入。
我尝试了不同的导入:
import 'rxjs/add/operator/map';
import 'rxjs/rx';
但我一直收到错误
http.post(...).map is not a function
更新 - 代码上下文
let body = "email=" + email + "&password=" + password;
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-from-urlencoded');
this.http.post('http://angular.app/api/v1/auth') // angular.app is laravel backend
.map( (responseData) => {
return responseData.json();
})
Angular2 beta.1 似乎需要 RxJS 5.0.0-beta.0。也许这是你的问题的原因。
如果我在 package
.json 文件中尝试这样做:
"dependencies": {
"angular2": "2.0.0-beta.1",
"systemjs": "0.19.6",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.1",
"zone.js": "0.5.10"
},
我有一个错误,Angular2 需要 RxJS 5.0.0-beta.0。
编辑
您需要在 bootstrap
函数的第二个参数中添加 HTTP_PROVIDERS
。
希望对你有帮助,
蒂埃里
对我来说 http.post(...).map()
按预期工作。
我确实需要导入 'rxjs/Rx'
import {Component} from 'angular2/core';
import {Http} from 'angular2/http';
import 'rxjs/Rx';
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<p>Test result {{result | json}}</p>
`
})
export class App implements OnInit{
public title = 'my title';
public result : String;
constructor(private _http : Http) {
_http.post('http://jsonplaceholder.typicode.com/posts')
.map(res => res.json())
.subscribe(
data => this.result = data,
err => console.log('ERROR!!!'),
() => console.log('Got response from API', this.result)
);
}
}
参见 plunker 示例:http://plnkr.co/edit/vise2zYxZUmr1kW65mNY?p=preview
希望这能帮助您找到问题
我已经查看了所有 github 问题和 Whosebug 帖子,但我无法让它工作
( https://github.com/angular/angular/issues/5632 )
(
- 我正在使用 Angular2@2.0.0-beta.1
- 当我在控制台中检查 console/resources 时,Rxjs (rxjs@5.0.0-beta.1) 已成功导入。
我尝试了不同的导入:
import 'rxjs/add/operator/map';
import 'rxjs/rx';
但我一直收到错误
http.post(...).map is not a function
更新 - 代码上下文
let body = "email=" + email + "&password=" + password;
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-from-urlencoded');
this.http.post('http://angular.app/api/v1/auth') // angular.app is laravel backend
.map( (responseData) => {
return responseData.json();
})
Angular2 beta.1 似乎需要 RxJS 5.0.0-beta.0。也许这是你的问题的原因。
如果我在 package
.json 文件中尝试这样做:
"dependencies": {
"angular2": "2.0.0-beta.1",
"systemjs": "0.19.6",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.1",
"zone.js": "0.5.10"
},
我有一个错误,Angular2 需要 RxJS 5.0.0-beta.0。
编辑
您需要在 bootstrap
函数的第二个参数中添加 HTTP_PROVIDERS
。
希望对你有帮助, 蒂埃里
对我来说 http.post(...).map()
按预期工作。
我确实需要导入 'rxjs/Rx'
import {Component} from 'angular2/core';
import {Http} from 'angular2/http';
import 'rxjs/Rx';
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<p>Test result {{result | json}}</p>
`
})
export class App implements OnInit{
public title = 'my title';
public result : String;
constructor(private _http : Http) {
_http.post('http://jsonplaceholder.typicode.com/posts')
.map(res => res.json())
.subscribe(
data => this.result = data,
err => console.log('ERROR!!!'),
() => console.log('Got response from API', this.result)
);
}
}
参见 plunker 示例:http://plnkr.co/edit/vise2zYxZUmr1kW65mNY?p=preview
希望这能帮助您找到问题