如何在打字稿Cordova项目中使用cordova插件? [英] How to use cordova plugin in a typescript cordova project?
问题描述
我有一个cordova + angular +打字稿项目,我正在其中尝试将juspay-ec-sdk-plugin用于Cordova.我在尝试过解决方案 Angular 4 Typescript应用程序中的Cordova插件 和 cordova插件无法解析为打字稿 但没有成功.如何在打字稿代码中引用此插件? PS:我已经尝试安装ngCordova类型,但是没有用.
I have a cordova +angular+ typescript project in which I'm trying to use the juspay-ec-sdk-plugin for Cordova. I've tried the solutions at Cordova plugin in Angular 4 Typescript application and cordova plugin cannot resolve in typescript but with no success. How can I refer to this plugin in my typescript code? PS: I've tried installing ngCordova typings but it didn't work.
推荐答案
我喜欢在我的主要组件app.component.ts中控制设备准备就绪,并使用服务来存储""cordova"
I like in my main component app.component.ts control de deviceready and use a service to "store" the "cordova"
有点像
declare var cordova: any; //<--declare "cordova"
declare var window:any; //<--declare "window"
//An enum of events
export enum CordovaEvent {BackButton,Resume,Pause}
//In constructor inject our "CordovaService", it's only to store cordova
constructor(private cordovaService: CordovaService){}
ngAfterViewInit() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
}
onDeviceReady() {
..here I have "cordova".., we can, e.g.
..and I have the pluggings...
cordova.getAppVersion.getVersionNumber().then(version => {
..make something with "version"
});
this.cordovaService.cordova=cordova //<--store "cordova" in a service
this.cordovaService.isCordoba = true; //<--store in a variable in a service if
//I'm in cordova or not
// we can control the 'pause','resume',backbutton...
document.addEventListener('pause', this.onPause.bind(this), false);
document.addEventListener('resume', this.onResume.bind(this), false);
document.addEventListener("backbutton", this.onBackKeyDown.bind(this), false);
};
onPause() {
//If our service has a function sendEvent
this.cordovaService.sendEvent(CordovaEvent.Pause);
};
onResume() {
this.cordovaService.sendEvent(CordovaEvent.Resume);
};
onBackKeyDown(e) {
this.cordovaEventService.sendEvent(CordovaEvent.BackButton);
e.preventDefault();
e.stopPropagation();
};
//我们的cordovaService
//Our cordovaService
export class CordovaService {
private listeningSource:Subject<CordovaEvent>=new Subject<CordovaEvent>();
cordovaEvent:Observable<CordovaEvent>=this.listeningSource.asObservable();
isCordoba:boolean=false;
cordova:any;
constructor() {
}
sendEvent(evento:CordovaEvent)
{
this.listeningSource.next(evento);
}
}
这篇关于如何在打字稿Cordova项目中使用cordova插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!