cordova 插件网络接口问题

cordova plugin network interface issue

我正在使用 ionic 2

已从 https://github.com/salbahra/cordova-plugin-networkinterface

下载插件

不能使用任何全局变量或在函数内部调用任何其他函数

networkinterface.getIPAddress(function (ip) { alert(ip);});

如果我用这样的东西

networkinterface.getIPAddress(function (ip) 
{ alert(ip);
this.test(ip); });

test(x){
console.log("IP = "+ x);}

我收到一个错误:

Error in Success callbackId: networkinterface1280836273 : TypeError: Cannot read property 'test' of null.

我正确地收到了 Ip 警报,但我无法在提供的功能之外访问它。 打字稿也给出了错误:

[ts] Cannot find name 'networkinterface'.

当我使用插件时。但它仍然可以编译和工作。

知道如何解决这个问题吗?

已解决..(感谢 ionic 社区)需要使用 lambda 函数 => 从周围的上下文中捕获 this 的含义。更多信息 here

loadIPAddress() {
    networkinterface.getIPAddress((ip) => {
      alert(ip);
      this.test(ip);
    });
  }

  test(x) {
    console.log("IP = "+ x);
  }