【问题标题】:cordova plugin network interface issue科尔多瓦插件网络接口问题
【发布时间】:2016-05-07 17:42:42
【问题描述】:

我正在使用离子 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);}

我得到一个错误:

成功 callbackId 出错:networkinterface1280836273:TypeError:无法读取 null 的属性“测试”。

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

[ts] 找不到名称“网络接口”。

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

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

【问题讨论】:

  • 我猜这个指针不是你想象的那样......看看this answer。我认为你和我犯了同样的错误。
  • 这些错误似乎是说插件没有完全存在,即使它仍然有效(奇怪)。我遇到过类似的问题,原因是没有添加插件。我会尝试手动将插件添加到项目中以确保(不确定如何使用 ionic 执行此操作)。
  • 感谢@Phonolog 的帮助
  • @ThomCunningham 没有正确安装插件。显然我需要对类型文件做一些事情。需要添加一些参考。仍在试图弄清楚如何删除 [ts] 找不到名称“网络接口”。虽然插件工作正常

标签: javascript cordova typescript ionic2


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 2014-12-29
    • 2015-10-13
    • 1970-01-01
    相关资源
    最近更新 更多