【问题标题】:Alert is not function error is console警报不是功能错误是控制台
【发布时间】:2018-07-12 16:19:57
【问题描述】:

我正在 ionic 3 项目中集成 otp 功能。当我发送 otp 时,我正在接收 otp 但由于此错误而没有重定向到 otp 接收页面。

以下是控制台错误 发送 otp 时出现以下错误

ERROR 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");
                });
        }
    }
}

【问题讨论】:

  • 重命名let alert并检查。

标签: javascript angular typescript ionic-framework ionic3


【解决方案1】:

您的第二个警报未定义

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

你需要像第一个那样替换它。

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

不过我还是建议你创建一个函数alert( message:string){},这样你就不会重复代码了。

【讨论】:

    猜你喜欢
    • 2016-11-28
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    相关资源
    最近更新 更多