使用Plugman创建离子插件 [英] Ionic Plugin Creation Using Plugman
问题描述
我是Ionic和Cordova的新手.我需要使用Cordova为ionic创建一个插件,并将其集成到示例ionic应用程序中.
I'm new to Ionic and Cordova. I need to create a plugin for ionic using Cordova and integrate it in sample ionic app.
我遵循的步骤是:
使用Plugman创建了一个简单的离子插件
Created a simple ionic plugin using plugman
plugman create --name SayHello --plugin_id cordova-plugin-sayhello -plugin_version 0.0.1
在上述插件中添加了android平台.
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
在Home.ts中的离子应用程序中,我编写了这段代码.
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.
就像我成功调用函数doSomething一样:
like if I call function doSomething from success:
var success = function(result) {
doSomething(result);
}
它显示错误doSomething函数未找到.它只能在控制台中打印.
It Shows Error doSomething function not found. 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));
}
}
这篇关于使用Plugman创建离子插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!