离子 - 吐司的消息不在里面工作' loaderror' inappbrowser的eventlistner函数

时间:2018-02-13 08:27:23

标签: android cordova ionic-framework inappbrowser

我尝试在给定网址无法加载inappbrowser时显示toast消息, 这是我的代码

function BrowserLoadError(event) {
        browser.close();
        loading.dismiss();
        this.toast.show("Can't Load the app", '3000', 'center').subscribe(
            toast => {
                console.log(toast);
            }
        );
        //alert("This App can't be reached now");
    }

但是它不起作用,toast消息没有显示,但是当我们从eventlistner函数以外的任何其他地方调用时它会显示。

如何让它在函数中运行?

这是我的完整代码

urlLink(data, target, options): void {
    let loading = this.loadingCtrl.create({
        spinner: 'bubbles',
        content: 'Processing',
        duration: 5000
    });

    let browser = cordova.InAppBrowser.open(data, target, options);

    browser.hide();
    loading.present();
    browser.addEventListener('loaderror', BrowserLoadError);

    function BrowserLoadError(event) {
        browser.close();
        loading.dismiss();
        this.toast.show("Can't Load the app", '3000', 'center').subscribe(
            toast => {
                console.log(toast);
            }
        );
        //alert("This App can't be reached now");
    }
}

3 个答案:

答案 0 :(得分:0)

经过一点测试后,问题实际上是this在错误回调中使用时不是你的控制器。将引用保存在变量中应解决此问题(双关语):

urlLink(data, target, options): void {
    let self = this; //save the reference before

    let loading = this.loadingCtrl.create({
        spinner: 'bubbles',
        content: 'Processing',
        duration: 5000
    });

    let browser = cordova.InAppBrowser.open(data, target, options);

    browser.hide();
    loading.present();
    browser.addEventListener('loaderror', BrowserLoadError);

    function BrowserLoadError(event) {
        browser.close();
        loading.dismiss();
        //use your variable here
        self.toast.show("Can't Load the app", '3000', 'center').subscribe(
            toast => {
                console.log(toast);
            }
        );
        //alert("This App can't be reached now");
    }
}

答案 1 :(得分:0)

与此同时,我也发现了这个问题,并且像这样修复了

let toast = new Toast();
toast.show("This App can't be launched now", '5000', 'bottom').subscribe(
    toast => {
        console.log(toast);
    }
);

答案 2 :(得分:-1)

_id: 1 - 1
awid: 1
officeid: 1
prname: ABCD
prfhname: Chevy
Ano: 555

_id: 1-2
awid: 1
officeid: 1
prname: bheegi
prfhname: Henry
Ano: 6555