我正在将otp功能集成到ionic 3项目中。当我发送otp时,我收到的是otp,但由于此错误,因此未重定向到otp会收到页面。

下面是控制台错误
在发送otp时遇到错误


  错误TypeError:警报不是功能
      在SafeSubscriber._error(home.ts:45)
      在SafeSubscriber .__ tryOrUnsub(Subscriber.js:238)
      在SafeSubscriber.error(Subscriber.js:197)
      在Subscriber._error(Subscriber.js:128)
      在Subscriber.error(Subscriber.js:102)
      在MapSubscriber._next(map.js:82)
      在MapSubscriber.Subscriber.next(Subscriber.js:89)
      在XMLHttpRequest.onLoad(http.js:1556)
      在t.invokeTask(polyfills.js:3)
      在Object.onInvokeTask(core.js:4620)


我正在接收otp,但是由于此错误,页面无法重定向到otp接收页面

我的代码:

import {Component} from '@angular/core';
import {NavController, Platform, AlertController} from 'ionic-angular';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
import {OtpReceivePage} from '../otp-receive/otp-receive';


@Component({
    selector: 'page-home',
    templateUrl: 'home.html'
})
export class HomePage {

    mobile = '';

    constructor(public alertCtrl: AlertController,
                public http: Http,
                public navCtrl: NavController) {
    }

    sendOTP() {
        if (this.mobile.length != 12) {
            let alert = this.alertCtrl.create({
                title: 'Mobile Number Required!',
                subTitle: 'Please enter your 10 digit mobile number with 91 country code!',
                buttons: ['OK']
            });
            alert.present();
        }
        else {
            this.http.get('http://localhost:8080/nexmosms/send-sms.php')
                .map(res => res.json())
                .subscribe(res => {
                    console.log(JSON.stringify(res));
                    this.navCtrl.push(OtpReceivePage, {mobileno: this.mobile})
                }, (err) => {
                    alert("failed");
                });
        }
    }
}

最佳答案

您的第二个警报未定义

(err) => {  alert("failed");});


您需要像在第一个契约中那样替换它。

let alert = this.alertCtrl.create({
                title: 'failed'
                buttons: ['OK']
            });
            alert.present();


但是再一次,我建议您创建一个函数alert( message:string){},以免重复代码。

关于javascript - 警报不是功能错误是控制台,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48578037/

10-12 07:00