简体   繁体   English

离子本机网络插件

[英]Ionic native network plugin

I need to check internet and id connection exist I need to perform a server request. 我需要检查互联网和ID连接是否存在,我需要执行服务器请求。 I used ionic native network Plugin. 我使用了离子本机网络插件。 But i done as per their official doc. 但是我按照他们的官方文件做了。

my CODE: 我的代码:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController } from 'ionic-angular';
import { Auth } from '../../providers/auth/auth';
import { Network } from '@ionic-native/network';
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {
  constructor(private network: Network,private alertCtrl: AlertController,public navCtrl: NavController, public navParams: NavParams,public authService: Auth, public loadingCtrl: LoadingController) {

}
  ionViewDidLoad() {
      // watch network for a disconnect
    let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
        console.log('network was disconnected :-(');
    });
  disconnectSubscription.unsubscribe();
  // watch network for a connection
  let connectSubscription = this.network.onConnect().subscribe(() => {
    console.log('network connected!');
    //Check if already authenticated
    this.authService.checkAuthentication().then((res) => {
        console.log("Already authorized");
        this.loading.dismiss();
        this.navCtrl.setRoot(HomePage);
    }, (err) => {
        console.log("Not already authorized");
        this.loading.dismiss();
    });
  });

  // stop connect watch
  connectSubscription.unsubscribe();
    console.log('ionViewDidLoad LoginPage');
  }
}

My console shows this error: 我的控制台显示此错误:

在此处输入图片说明

Why does this happen? 为什么会这样? Is it necessary to add in providers array? 是否需要添加provider数组?

UPDATE UPDATE

As Daniel B mentioned i added Network in providers array and the above error is gone. 正如Daniel B提到的,我将网络添加到了提供程序数组中,以上错误消失了。 Now it didn't get inside this.network.onConnect() 现在它没有进入this.network.onConnect()

您必须将Network添加到*.module.tsapp.module.ts )中的providers数组中。

You need to add Network as a provider. 您需要将Network添加为提供者。

To do this your app.module.ts add Network as a provider: 为此,您的app.module.tsNetwork添加为提供者:

import { Network } from '@ionic-native/network';

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    ...
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    Network
  ]
})

However, as the Network module is part of ionic-native it requires Cordova and therefore won't run in a browser, only in an emulator or natively. 但是,由于Network模块是ionic-native一部分,因此它需要Cordova,因此不会在浏览器中运行,只能在仿真器或本机中运行。

I suggest wrapping the functions using the Network provider platform ready so it doesn't crash the browser but still works when there is cordova: 我建议使用Network provider平台包装功能,以免浏览器崩溃,但在存在cordova时仍然可以使用:

this.platform.ready().then(() => {
    // calls to ionic-native modules e.g. `Network` methods
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM