简体   繁体   English

使用 Plugman 创建 Ionic 插件

[英]Ionic Plugin Creation Using Plugman

I'm new to Ionic and Cordova.我是 Ionic 和 Cordova 的新手。 I need to create a plugin for ionic using Cordova and integrate it in sample ionic app.我需要使用 Cordova 为 ionic 创建一个插件并将其集成到示例 ionic 应用程序中。

Steps I Followed are:我遵循的步骤是:

Created a simple ionic plugin using plugman使用插件程序创建了一个简单的离子插件

plugman create --name SayHello --plugin_id cordova-plugin-sayhello -plugin_version 0.0.1

Added android platform to above plugin.在上述插件中添加了 android 平台。

cd SayHello/ && plugman platform add --platform_name android

Now I want to integrate this plugin into my ionic app.现在我想将此插件集成到我的 ionic 应用程序中。

ionic cordova plugin add ../SayHello

In my ionic app inside Home.ts, I wrote this piece of code.在 Home.ts 中的 ionic 应用程序中,我编写了这段代码。

declare var cordova: any;
var success = function(result) {
  console.log(result);
}
var failure = function(err) {
  console.log(err);
}
cordova.plugins.HelloWorld.coolMethod("SayHelloTest", success, failure);

The problem is I cannot call any function from success or failure in the ionic app.问题是我无法在 ionic 应用程序中调用任何成功或失败的函数。

like if I call function doSomething from success:就像我从成功调用函数 doSomething 一样:

var success = function(result) {
   doSomething(result);
}

It Shows Error doSomething function not found.它显示错误 doSomething 函数未找到。 It can only print in console.它只能在控制台中打印。

you need to create success as the class function and either send it as a bound function or call inside arrow.您需要将成功创建为类函数,并将其作为绑定函数发送或在箭头内调用。

declare var cordova:any;

class HomePage{
    //constructor etc...
    doSomething(res:any){
    }

    success(result){
        this.doSomething(result);
    }
    failure(err){}
    //..
    //call
    callCordovaFunction(){
        cordova.plugins.HelloWorld.coolMethod("SayHelloTest", this.success.bind(this), this.failure.bind(this));
    //or
        cordova.plugins.HelloWorld.coolMethod("SayHelloTest", (res)=>this.success(res),(err)=>this.failure(err));    
    }
}

After creating the plugin, it is theoretically possible to import it also with plugman in order to do what you're trying to do.创建插件后,理论上也可以使用插件程序导入它,以便执行您要执行的操作。 I have read the command is: plugman install --platform android --project projectPlatformPath --plugin pluginPath我读过的命令是: plugman install --platform android --project projectPlatformPath --plugin pluginPath

Anyway, this didn't work for me when I tried and also makes your plugin uncomfortable to use.无论如何,当我尝试时这对我不起作用,并且还使您的插件使用起来不舒服。 It is probably a better idea to create an ionic wrapper for your plugin with gulp and copy it into your project's node_modules/@ionic-native .这可能是一个更好的主意来创建你的插件离子包皮gulp ,并将其复制到你的项目的node_modules/@ionic-native This way you would be able to inject it like the other plugins you just add with ionic cordova plugin add cordova-plugin-name-here .这样,您就可以像使用ionic cordova plugin add cordova-plugin-name-here添加的其他插件一样注入它。 This is also the recommended way by Ionic.这也是 Ionic 推荐的方式。

Detailed instructions would be long to write here.在这里写下详细说明会很长。 Just visit this tutorial and follow the step-by-step instructions.只需访问本教程并按照分步说明进行操作即可。

To install plugman, you must have node installed on your machine.要安装插件程序,您必须在您的机器上安装节点 Then you can run the following command from anywhere in your environment to install plugman globally, so that it is available from any directory:然后你可以在你的环境中的任何地方运行以下命令来全局安装plugman,以便它可以从任何目录使用:

$ npm install -g plugman

Once you have installed Plugman and have created a Cordova project, you can start adding plugins to the platform with:一旦你安装了 Plugman 并创建了一个 Cordova 项目,你就可以开始向平台添加插件:

$ plugman create --name SayHello --plugin_id cordova-plugin-sayhello -plugin_version 0.0.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM