如果有網絡連接但互聯網不工作。例如有一個WiFi連接,但WiFi沒有互聯網連接,瀏覽器觀察者應該觸發「loaderror」事件。ionic2 InAppBrowser(離子本機)loaderror不起作用
代碼:
import { Component } from '@angular/core';
import { Platform,AlertController,LoadingController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Network } from '@ionic-native/network';
import { HomePage } from '../pages/home/home';
import { InAppBrowser } from '@ionic-native/in-app-browser';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
public rootPage:any = HomePage;
constructor(private platform: Platform,private statusBar: StatusBar,private splashScreen: SplashScreen, private network: Network, private alertCtrl: AlertController, private iab: InAppBrowser, private loadingCtrl: LoadingController) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
let alert = this.alertCtrl.create({
title: "Network Problem",
subTitle: 'Internet is not connected' ,
buttons: [
{
text: 'Close', handler:() => {
this.platform.exitApp();
}
}
]
});
const loading = this.loadingCtrl.create();
if(this.network.type !== 'none') {
loading.present();
const browser = this.iab.create('http://www.google.com','_blank','location=no,clearsessioncache=yes');
browser.on("loaderror").subscribe(() => {
loading.dismiss();
this.iab.create('http://192.168.2.222/users/dashboard','_blank','location=no,clearsesioncache=yes');
});
const sub = browser.on('loadstart').subscribe(() => {
loading.dismiss();
sub.unsubscribe();
},
err => { loading.dismiss(); console.log('error') ; alert.present()},
() => { loading.dismiss(); console.log('success') });
}
/// check connectivity automatically .
let connectSubscription = network.onConnect().subscribe(() => {
loading.present();
const browser = this.iab.create('http://www.google.com','_blank','location=no,clearsesioncache=yes');
browser.on("loaderror").subscribe(() => {
loading.dismiss();
this.iab.create('http://192.168.2.222/users/dashboard','_blank','location=no,clearsesioncache=yes');
});
});
let disconnectSubscription = network.onDisconnect().subscribe(() => {
const browser = this.iab.create("error.html",'_blank','location=no');
});
statusBar.styleDefault();
splashScreen.hide();
});
}
}
但它總是加載谷歌網頁,甚至都能得到加載clearsessioncache和網絡斷開後。我在android和桌面上測試了這個。
當url有問題時發生了什麼? – Sampath
什麼都不顯示白頁,只在那裏。 –
我改變了加載錯誤的代碼我的火腳本,它得到加載,但10秒後。當我啓動我的應用程序需要10秒,當loaderror發生時,它再次需要10秒,所以共20。 –