我已经创建了cordova插件,并且已经在Ionic 1中使用了,它可以正常工作。然后我尝试在Ionic 2中使用它,但我真的不知道如何调用该插件。我从这里开始创建自己的插件。这就是我所做的:
plugin.xml
<name>myPlugin</name>
<js-module src="www/myPlugin.js" name="myPlugin">
<clobbers target="myPlugin" />
</js-module>
myPlugin.js
module.exports = {
myFunction: function (success, failure) {
cordova.exec(success, failure, "myPlugin", "myFunction", []);
}
};
你好
import { Component } from '@angular/core';
declare var cordova: any;
@Component({
selector: 'page-hello-ionic',
templateUrl: 'hello-ionic.html'
})
export class HelloIonicPage {
constructor() {
}
click() {
if (typeof cordova !== 'undefined') {
cordova.plugins.myPlugin.myFunction();
}
}
}
但不幸的是它返回我一个错误"Undefined myFunction"
在hello-ionic.ts
。
这是我所做的。
你好
import { Component } from '@angular/core';
declare var myPlugin: any;
@Component({
selector: 'page-hello-ionic',
templateUrl: 'hello-ionic.html'
})
export class HelloIonicPage {
constructor() {
}
click() {
myPlugin.myFuntion(
(data) => {
console.log(data);
},
(err) => {
console.log(err);
});
}
}
declare var myPlugin: any;
,myPlugin
我的名字来源于<clobbers target="myPlugin" />
。
注意:仅需要在设备中运行项目。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句