Ionic Plugin Creation Using Plugman

不羁岁月 提交于 2019-12-11 02:53:00

问题


I'm new to Ionic and Cordova. I need to create a plugin for ionic using Cordova and integrate it in sample ionic app.

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.

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

Now I want to integrate this plugin into my ionic app.

ionic cordova plugin add ../SayHello

In my ionic app inside Home.ts, I wrote this piece of code.

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.

like if I call function doSomething from success:

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

It Shows Error doSomething function not found. It can only print in console.


回答1:


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));    
    }
}



回答2:


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

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. 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. This is also the recommended way by Ionic.

Detailed instructions would be long to write here. Just visit this tutorial and follow the step-by-step instructions.



来源:https://stackoverflow.com/questions/53113715/ionic-plugin-creation-using-plugman

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!