Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

cordova - 使用Plugman创建离子插件(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创建了一个简单的离子插件)

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 cordova plugin add ../SayHello

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

(在Home.ts中的离子应用程序中,我编写了这段代码。)

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:

(就像我成功调用函数doSomething一样:)

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

It Shows Error doSomething function not found.

(它显示错误doSomething函数未找到。)

It can only print in console.

(它只能在控制台中打印。)

  ask by Saurabh Pandey translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...