当前位置:  开发笔记 > 编程语言 > 正文

Http.post错误:map不是Angular 2中的函数

如何解决《Http.post错误:map不是Angular2中的函数》经验,为你挑选了1个好方法。

我正试图通过http.post函数从url获取数据.但是我收到了错误.app.component.ts文件的源代码:

import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {Http, HTTP_PROVIDERS, Headers} from 'angular2/http';
@Component({
selector: 'my-app',
template: `
 

Posted the following values:

{{postResponse.id}}
{{postResponse.name}}
` }) export class AppComponent { result: Object; http: Http; postResponse = new Person(); constructor(http: Http) { this.http = http; } postData(){ var headers = new Headers(); headers.append('Content-Type', 'application/json'); this.http.post('http://141.79.39.454:8008/the-link-something-like-this...', JSON.stringify({"id": 1, "name": "?????"}),{headers:headers}) .map(res => res.json()) .subscribe((res:Person) => this.postResponse = res); } } class Person{ id:number; name:string; } bootstrap(AppComponent, [HTTP_PROVIDERS]);

url中的JSON看起来像这样. 当我点击按钮时我得到的错误如下:

EXCEPTION: Error during evaluation of "click"
Uncaught EXCEPTION: Error during evaluation of "click"
ORIGINAL EXCEPTION: TypeError: this.http.post(...).map is not a function
ORIGINAL STACKTRACE:
TypeError: this.http.post(...).map is not a function
at AppComponent.postData (http://localhost:3000/app/app.component.js:35:26)
at AbstractChangeDetector.ChangeDetector_AppComponent_0.handleEventInternal (eval at  (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:10897:14), :124:33)
at AbstractChangeDetector.handleEvent (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:8788:22)
at AppView.dispatchEvent (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9396:39)
at AppView.dispatchRenderEvent (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9391:19)
at DefaultRenderView.dispatchRenderEvent (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:7819:53)
at eventDispatcher (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9781:19)
at http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:9852:14
at http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:15370:34
at http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:7874:18
ERROR CONTEXT:
[object Object]run @ angular2-polyfills.js:143zoneBoundFn @ angular2-polyfills.js:111

Thierry Temp.. 12

也许您可以尝试在导入中添加以下内容:

import 'rxjs/add/operator/map';

有关更多详细信息,请参阅此问题:https://github.com/angular/angular/issues/5632.

希望它对你有帮助,蒂埃里



1> Thierry Temp..:

也许您可以尝试在导入中添加以下内容:

import 'rxjs/add/operator/map';

有关更多详细信息,请参阅此问题:https://github.com/angular/angular/issues/5632.

希望它对你有帮助,蒂埃里

推荐阅读
跟我搞对象吧
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有