2017-07-23 102 views
1

這裏我導入插件,它在這裏一切正常。科爾多瓦網絡檢測插件沒有響應

ionic plugin add cordova-plugin-network-information 

npm install --save @ionic-native/network 

它添加到app.module.ts,並宣佈它作爲一個供應商...

import { NgModule, ErrorHandler } from '@angular/core'; 
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular'; 
import { MyApp } from './app.component'; 
import { HomePage } from '../pages/home/home'; 
import { StatusBar } from '@ionic-native/status-bar'; 
import { SplashScreen } from '@ionic-native/splash-screen'; 
import { Network } from '@ionic-native/network'; 

@NgModule({ 
    declarations: [ 
    MyApp, 
    HomePage 
    ], 
    imports: [ 
    IonicModule.forRoot(MyApp) 
    ], 
    bootstrap: [IonicApp], 
    entryComponents: [ 
    MyApp, 
    HomePage 
    ], 
    providers: [ 
    StatusBar, 
    SplashScreen, 
    {provide: ErrorHandler, useClass: IonicErrorHandler}, 
    Network 
    ] 
}) 
export class AppModule {} 

注射在構造函數中,現在我覺得這裏是我現在用的問題this.network.onConnect();

import { NavController, ToastController } from 'ionic-angular'; 
import { Network } from '@ionic-native/network'; 

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

    constructor(private toast: ToastController, private network: Network, public navCtrl: NavController) { 
    } 
} 

ionViewDidEnter() { 
    this.network.onConnect().subscribe(data => { 
    console.log(data) 
    }, error => console.error(error)); 

    this.network.onDisconnect().subscribe(data => { 
    console.log(data) 
    }, error => console.error(error)); 
} 

我帶wifi玩開啓/關閉,但沒有登錄控制檯.. 而且我知道我做的一切權利,但仍?

回答

0

ionViewDidEnter應該在HomePage類的內部。

import { NavController, ToastController } from 'ionic-angular'; 
import { Network } from '@ionic-native/network'; 

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

    constructor(private toast: ToastController, private network: Network, public navCtrl: NavController) {} 


    ionViewDidEnter() { 
     this.network.onConnect().subscribe(data => { 
      console.log(data) 
     }, error => console.error(error)); 

     this.network.onDisconnect().subscribe(data => { 
      console.log(data) 
     }, error => console.error(error)); 
    } 
} 
+0

我沒有收到任何錯誤... – Nakomoto

+0

從答案複製代碼並將其粘貼到用戶home.ts文件 –

+0

更新的答案,說語法錯誤是不是在這種情況下是正確的。讓我知道它是否有效。 –

相關問題