Ionic 2 TypeError:this.http.post不是函数 [英] Ionic 2 TypeError: this.http.post is not a function

查看:243
本文介绍了Ionic 2 TypeError:this.http.post不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi i'm trying to make a signup page in ionic 2 that post username, email and password to the api that i use. But i get this error TypeError: this.http.post is not a function. I've already search about this problem and try but it doesn't work for me.

这是我的signup.js:

Here's my 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');
    }
}

请帮助!

推荐答案

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

I think that it's because the order of providers you use at the level of the parameters getter and the constructor:

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

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

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

You need to define the same order at both levels (and also square brackets the right way), as described below:

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

constructor(nav, userData, http) {
}

这篇关于Ionic 2 TypeError:this.http.post不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆