Ionic 2 TypeError:this.http.post不是函数

时间:2016-05-03 10:44:19

标签: ionic-framework angular ionic2

您好我正在尝试在离子2中创建一个注册页面,将用户名,电子邮件和密码发布到我使用的API。但我收到此错误TypeError: this.http.post is not a function。我已经搜索过这个问题并尝试但它对我不起作用。

这是我的signup.js:

import {Injectable, Inject} from 'angular2/core';
import {Page, NavController} from 'ionic-angular';
import {TabsPage} from '../tabs/tabs';
import {UserData} from '../../providers/user-data';
import {Http, Headers, RequestOptions} from 'angular2/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/Rx';

@Page({
  templateUrl: 'build/pages/signup/signup.html'
})
@Injectable()
export class SignupPage {
  static get parameters() {
    return [[Http], [NavController], [UserData]];
  }

  constructor(nav, userData, http) {
    this.nav = nav;
    this.userData = userData;
    this.http = http;

    this.signup = {};
    this.submitted = false;
  }

  onSignup(form) {
    let body = JSON.stringify(form.value);
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Accept', 'application/json');
    let options = new RequestOptions({ headers: headers });
    this.http.post('http://localhost:8000/api/v1/auth/register', body, options)
        .map(res => res.json())
        .subscribe(
            data => this.success = data
        )
        .catch(this.handleError);
  }

  handleError(error) {
        console.error(error);
        return Observable.throw(error.json().error || 'Server error');
    }
}

请帮忙!

1 个答案:

答案 0 :(得分:2)

我认为这是因为您在parameters getter和构造函数级别使用的提供程序的顺序:

static get parameters() {
  return [[Http], [NavController], [UserData]]; // <-----
}

constructor(nav, userData, http) { // <-----
}

您需要在两个级别定义相同的顺序(以及正确的方括号),如下所述:

static get parameters() {
  return [[NavController, UserData, Http]]; // <-----
}

constructor(nav, userData, http) {
}