将非原生 cordova 插件的值传回 ionic 4

Pass value from non-native cordova plugin back to ionic 4

我无法将非本地 cordova 插件的值传回 ionic。我正在使用 ionic 4。当我在 cordova 插件的事件监听器中时,我也无法使用路由器。

var browser = cordova.InAppBrowser.open("XXXX, '_blank', 'location=no,clearsessioncache=yes,clearcache=yes');
browser.addEventListener('loadstop', function (e) {
    if (e.url.indexOf('XX') != -1) {
        browser.hide();
        this.router.navigate([Constants.DASHBOARD]);
    }
});

未捕获以下命中错误:

TypeError: Cannot read property 'navigate' of undefined

改变

browser.addEventListener('loadstop', function (e) {

browser.addEventListener('loadstop', e => {

箭头函数保持其父级的作用域。使用函数定义它自己的范围。因此,使用箭头函数 this 将指向具有路由器的父级。有一个函数,this指向没有路由的函数。

来自documentation

An arrow function does not have its own this. The this value of the enclosing lexical scope is used; arrow functions follow the normal variable lookup rules. So while searching for this which is not present in current scope, an arrow function ends up finding the this from its enclosing scope.