How to use cordova plugin in a typescript cordova project?

前端 未结 1 503
执念已碎
执念已碎 2021-01-25 21:05

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 Typescr

相关标签:
1条回答
  • 2021-01-25 21:54

    I like in my main component app.component.ts control de deviceready and use a service to "store" the "cordova"

    Some like

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

    //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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题