【问题标题】:Checking network connection with alert in Ionic 2在 Ionic 2 中使用警报检查网络连接
【发布时间】:2017-06-23 23:20:30
【问题描述】:

尝试使用 Ionic 2 中的警报检查网络连接。This tutorial 很棒,但现在已经很老了,从那时起 Ionic 2 语法发生了变化。 cmets 中的 Etir 提到编辑我已经完成的 Alert 组件,但仍然输出错误。有什么想法吗?

HomePage.ts

import {Component} from '@angular/core';
import {NavController, AlertController, Platform} from 'ionic-angular';

declare var navigator: any;
declare var Connection: any;

@Component({
    templateUrl: 'build/pages/home/home.html'
})
export class HomePage {

    constructor(private navCtrl: NavController, private platform: Platform, public alertCtrl: AlertController) { }

    checkNetwork() {
        this.platform.ready().then(() => {
            var networkState = navigator.connection.type;
            var states = {};
            states[Connection.UNKNOWN]  = 'Unknown connection';
            states[Connection.ETHERNET] = 'Ethernet connection';
            states[Connection.WIFI]     = 'WiFi connection';
            states[Connection.CELL_2G]  = 'Cell 2G connection';
            states[Connection.CELL_3G]  = 'Cell 3G connection';
            states[Connection.CELL_4G]  = 'Cell 4G connection';
            states[Connection.CELL]     = 'Cell generic connection';
            states[Connection.NONE]     = 'No network connection';
            let alert = alertCtrl.create({
                title: "Connection Status",
                subTitle: states[networkState],
                buttons: ["OK"]
            });
            this.navCtrl.present(alert);
        });
    }

}

home.html

<ion-header>
    <ion-navbar>
        <ion-title>
            Ionic Network Check
        </ion-title>
    </ion-navbar>
</ion-header>

<ion-content padding>
    <button (click)="checkNetwork()">Check Network</button>
</ion-content>

在 home.ts 中返回的错误

找不到名称“alertCtrl”- 第 26 行 'NavController' 类型上不存在属性'present' - 第 31 行

【问题讨论】:

    标签: javascript angular typescript ionic-framework ionic2


    【解决方案1】:

    您的页面定义中有一些拼写错误:

    import {Component} from '@angular/core';
    import {NavController, AlertController, Platform} from 'ionic-angular';
    
    declare var navigator: any;
    declare var Connection: any;
    
    @Component({
        templateUrl: 'build/pages/home/home.html'
    })
    export class HomePage {
    
        constructor(private navCtrl: NavController, private platform: Platform, public alertCtrl: AlertController) { }
    
        checkNetwork() {
            this.platform.ready().then(() => {
                var networkState = navigator.connection.type;
                var states = {};
                states[Connection.UNKNOWN]  = 'Unknown connection';
                states[Connection.ETHERNET] = 'Ethernet connection';
                states[Connection.WIFI]     = 'WiFi connection';
                states[Connection.CELL_2G]  = 'Cell 2G connection';
                states[Connection.CELL_3G]  = 'Cell 3G connection';
                states[Connection.CELL_4G]  = 'Cell 4G connection';
                states[Connection.CELL]     = 'Cell generic connection';
                states[Connection.NONE]     = 'No network connection';
    
                // this. was missing
                let alert = this.alertCtrl.create({
                    title: "Connection Status",
                    subTitle: states[networkState],
                    buttons: ["OK"]
                });
    
                // This is the proper way to present the alert
                alert.present();
            });
        }
    
    }
    

    组件文档给出了一个完整的例子:

    https://ionicframework.com/docs/v2/components/#alert

    【讨论】:

    • 错误已经消失,但现在出现空白屏幕
    • @merch89 您是否在控制台中遇到任何错误?
    • 已修复,@Component 引用不正确。从过时的教程中提取。正在工作,谢谢。
    • 这个解决方案可以用来跟踪网络连接的变化。有没有其他方法可以在初始加载应用程序时检查连接状态
    猜你喜欢
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多