相关疑难解决方法(0)

为ionic2项目创建自定义cordova插件

我们中的许多人都会经历类似的问题,但即使经过以下大多数相关链接引用link1参考link2,我也无法解决.

问题:

创建一个自定义插件(Cordova)以便在ionic2项目中使用它.

期望:此插件将能够与IOS和Android的本机功能进行交互.确切地说,我正在尝试使用cordova访问Ionic项目的本机SDK(Aruba内部定位SDK)的功能.

步骤1根据参考链接1初始创建插件

第2步创建Ionic 2项目(使用基本步骤创建)

步骤3插件中的JavaScript文件无法在Ionic2中引用和访问.

谷歌搜索后,我发现了这个讨论,由于以下原因,它被告知在插件中创建界面.

从'../../../plugins/xxx/*.js'导入{myPluginName}

因为插件不是离子本机包的一部分而无法工作.

如果你有自定义插件,你可以做一些事情.

1)制作PR以将其添加到离子原生物中

2)使用原始插件API.您可以使用原始插件API,而不必将其作为Ionic Native的一部分.该插件位于窗口对象上,因此您可以正常定位api

window.plugin.myPlugin.myMethod()

根据GITHUB Example项目,这种方式应该实现接口

interface CordovaPlugins {
  ZPLPrinter: ZPLPrinter;
}

interface ZPLPrinter {

  print(
    ipaddress: string,
    bclabels: any,
    printSuccess: (ip: string, labels: string[]) => void,
    printError: (message: string) => void): void;

}
Run Code Online (Sandbox Code Playgroud)

现在我在我的插件中创建了一个类似的界面,插件的www文件夹中有以下内容

interface CordovaPlugins {
  Communicator: Communicator;
}

interface Communicator {

  getInfo(successCallback: any, errorCallback: any);

}
Run Code Online (Sandbox Code Playgroud)

理想情况下,此接口将在JS文件中定位此方法

Device.prototype.getInfo = …
Run Code Online (Sandbox Code Playgroud)

javascript plugins android cordova ionic2

4
推荐指数
1
解决办法
5983
查看次数

标签 统计

android ×1

cordova ×1

ionic2 ×1

javascript ×1

plugins ×1