嗨我'm new to ionic 3 & angular 4 and I' M试图创建一个登录页面,将电子邮件和密码发布到api . 但我得到这个错误"this.http.post is not a function" . 我一直在寻找,但我没有设法解决问题 . 我已经阅读了这篇文章With ionic 2,但我认为这不是同一个问题 .
这是我的app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { HttpModule, Headers, RequestOptions } from '@angular/http';
import 'rxjs/Rx';
import { MyApp } from './app.component';
import { LoginPage } from '../pages/login/login';
import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
@NgModule({
declarations: [
MyApp,
HomePage,
ListPage,
LoginPage
],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
ListPage,
LoginPage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
这是我的login.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { FormBuilder, FormGroup, Validators} from '@angular/forms';
import { HttpModule, Headers, RequestOptions } from '@angular/http';
import { HomePage } from '../home/home';
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage {
isLogged: boolean;
constructor(public navCtrl: NavController,public fb: FormBuilder, public http: HttpModule) {
this.myForm = this.fb.group({
email: ['', [Validators.required]],
password: ['', [Validators.required]]
});
this.http = http;
}
loginUser(){
var datajson= {accion: 'login', email: this.myForm.value.email, password: this.myForm.value.password};
alert(JSON.stringify(datajson));
var apiurl = 'http://batbike.es/ajax/bbdd.php';
this.http.post(apiurl,datajson).then(data => {alert(data.data)}).catch(error=>{ alert (error.status)});
//Esta linea me lleva a la página home
//this.navCtrl.setRoot(HomePage).then(data => console.log(data)),error => console.log(error);
}
}
在这两个页面上我导入了http模块,但是我不能使用方法post . 你看到错误吗?非常感谢你
2 回答
正如@JB Nizet所说,你正在注入
HttpModule
而不是Http
. 你也应该使用HttpClient
这是已经取代Http
的新版本 .那么让我们做那些改变吧 .
app.module.ts
login.ts
:)流行的问题,使用ReflectiveInjector来创建你的'http'对象