【发布时间】:2017-11-01 22:31:34
【问题描述】:
您好,我是 ionic 3 和 angular 4 的新手,我正在尝试创建一个登录页面,将电子邮件和密码发布到 api。但我收到此错误“this.http.post 不是函数”。 我一直在寻找,但我没有设法解决问题。 我已经阅读了这篇帖子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 方法。 你看到错误了吗?非常感谢
【问题讨论】:
-
您注入 HttpModule 而不是在组件中注入 Http。请注意 Http 在 Angular 5 中被弃用,取而代之的是 HttpClient。