2017-09-19 11 views
0

ionic2를 배우고 있습니다. 내가 는 OnConnect()와 OnDisconnect의() 메소드에서 모든 로그 또는 경고를받지 못했습니다 나누었다 나는 .I이 링크 Ionic2 Network Connection에서 코드를 불리는 네트워크 연결 함께 일하고 있어요.Ionic2 네트워크 연결 OnConnect() 및 onDisconnect() 메서드 내부에 로그가 표시되지 않습니다.

.TS 파일 (코드)

import { Component } from '@angular/core'; 
import { NavController, NavParams, IonicPage } from 'ionic-angular'; 
import { ToastController } from 'ionic-angular'; 
import { Http } from '@angular/http'; 
import { LoadingController } from 'ionic-angular'; 
import { Storage } from '@ionic/storage'; 
import { NativeStorage } from '@ionic-native/native-storage'; 
import { AlertController } from 'ionic-angular'; 
import { Ionic2RatingModule } from 'ionic2-rating'; 
import 'rxjs/add/operator/map'; 
import { Network } from '@ionic-native/network'; 

@IonicPage() 
@Component({ 
    selector: 'page-outlet', 
    templateUrl: 'outlet.html', 
}) 
export class OutletPage { 
    constructor(private storage: Storage,public navParams: NavParams,public alertCtrl: AlertController,public http: Http,public loadCtrl: LoadingController,public toastCtrl: ToastController,public nativeStorage: NativeStorage, public nav: NavController,private network: Network) { 
    this.loaddata(); 

      var networkState = this.network.type; 
      let alert1 = this.alertCtrl.create({ 
       title: "Connection Status", 
       subTitle: networkState, 
       buttons: ["OK"] 
      }); 
      alert1.present(); 


     // watch network for a disconnect 
     let disconnectSubscription = this.network.onDisconnect().subscribe(() => { 
     console.log('network was disconnected :-('); 
     }); 

     // stop disconnect watch 
     disconnectSubscription.unsubscribe(); 

     // watch network for a connection 
     let connectSubscription = this.network.onConnect().subscribe(() => { 
     console.log('network connected!'); 
     // We just got a connection but we need to wait briefly 
     // before we determine the connection type. Might need to wait. 
     // prior to doing any api requests as well. 

     setTimeout(() => { 
     console.log(this.network.type); 
     if (this.network.type === 'wifi') { 
     alert('we got a wifi connection, woohoo!'); 
      } 
     }, 3000); 
     }); 

     // stop connect watch 
     connectSubscription.unsubscribe(); 

     } 

     loaddata(){ 
       let headers = new Headers({ 'Content-Type': 'application/json' }); 

       this.http.get("http://aryvartdev.com/reload/api/ApiController/categorylist",headers) 
       .map(res => res.json()) 
       .subscribe(data => { 
       this.rescall=data.message.product_category; 
       console.log("-this-"+JSON.stringify(this.rescall)); 
       this.rows = Array.from(Array(Math.ceil(this.rescall.length/2)).keys()); 
         this.rowsLen=this.rows.length; 
      console.log("row--len--"+this.rowsLen);  
       }, error => { 

       console.log(error);// Error getting the data 
       }); 

      } 
     } 

내 .TS 생성자 메서드 내에서이 코드를 준하고는 잘 작동. 네트워크 유형 만 표시합니다.

왜이 두 가지 방법이 효과가 없는지 아무도 알아 줄 수 있습니까?

+0

당신은 그래 난 내 코드 –

+0

를 배치 할 수 있습니다. 그래서 작동하지 않는 당신은 중지 된 연결 감시 메서드'disconnectSubscription.unsubscribe()'및'connectSubscription.unsubscribe(); ' – NandhiniB

+1

@NandhiniB 문제가 당신이 연결 시계 방법을 들렸다이라고 업데이 트됩니다 전체 코드 –

답변

1
import { Component } from '@angular/core'; 
    import { NavController, NavParams, IonicPage } from 'ionic-angular'; 
    import { ToastController } from 'ionic-angular'; 
    import { Http } from '@angular/http'; 
    import { LoadingController } from 'ionic-angular'; 
    import { Storage } from '@ionic/storage'; 
    import { NativeStorage } from '@ionic-native/native-storage'; 
    import { AlertController } from 'ionic-angular'; 
    import { Ionic2RatingModule } from 'ionic2-rating'; 
    import 'rxjs/add/operator/map'; 
    import { Network } from '@ionic-native/network'; 

    @IonicPage() 
    @Component({ 
     selector: 'page-outlet', 
     templateUrl: 'outlet.html', 
    }) 
    export class OutletPage { 
     constructor(private storage: Storage,public navParams: NavParams,public alertCtrl: AlertController,public http: Http,public loadCtrl: LoadingController,public toastCtrl: ToastController,public nativeStorage: NativeStorage, public nav: NavController,private network: Network) { 
     this.loaddata(); 

       var networkState = this.network.type; 
       this.presentAlert(networkState) 


      // watch network for a disconnect 
      let disconnectSubscription = this.network.onDisconnect().subscribe(() => { 
      this.presentAlert('network was disconnected :-('); 
      }); 

      // stop disconnect watch 
      //disconnectSubscription.unsubscribe(); 

      // watch network for a connection 
      let connectSubscription = this.network.onConnect().subscribe(() => { 
      this.presentAlert('network connected!'); 
      // We just got a connection but we need to wait briefly 
      // before we determine the connection type. Might need to wait. 
      // prior to doing any api requests as well. 

      setTimeout(() => { 
      this.presentAlert(this.network.type); 
      if (this.network.type === 'wifi') { 
      this.presentAlert('we got a wifi connection, woohoo!'); 
       } 
      }, 3000); 
      }); 

      // stop connect watch 
      //connectSubscription.unsubscribe(); 

      } 

      loaddata(){ 
        let headers = new Headers({ 'Content-Type': 'application/json' }); 

        this.http.get("http://aryvartdev.com/reload/api/ApiController/categorylist",headers) 
        .map(res => res.json()) 
        .subscribe(data => { 
        this.rescall=data.message.product_category; 
        console.log("-this-"+JSON.stringify(this.rescall)); 
        this.rows = Array.from(Array(Math.ceil(this.rescall.length/2)).keys()); 
          this.rowsLen=this.rows.length; 
       console.log("row--len--"+this.rowsLen);  
        }, error => { 

        console.log(error);// Error getting the data 
        }); 

       } 
      } 

presentAlert(type) { 
    let alert = this.alertCtrl.create({ 
    title: 'You are connected to ', 
    subTitle: type, 
    buttons: ['Dismiss'] 
    }); 
    alert.present(); 
} 
+0

고마워요 ... 코드가 저에게 효과적이었습니다. 감사합니다 – NandhiniB

+0

나는 그것이 당신을 위해 일해서 다행입니다. –