【问题标题】:How to use cordova plugin in a typescript cordova project?如何在打字稿cordova项目中使用cordova插件?
【发布时间】:2019-02-05 15:48:16
【问题描述】:

我有一个 cordova +angular+ typescript 项目,我正在尝试将 juspay-ec-sdk-plugin 用于 Cordova。我已经尝试了解决方案 Cordova plugin in Angular 4 Typescript applicationcordova plugin cannot resolve in typescript 但没有成功。如何在我的打字稿代码中引用这个插件? PS:我尝试安装 ngCordova 类型,但没有成功。

【问题讨论】:

    标签: angular typescript cordova


    【解决方案1】:

    我喜欢在我的主要组件 app.component.ts 控制 de deviceready 并使用服务来“存储”“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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-11
      • 2017-06-25
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 2015-03-07
      • 1970-01-01
      相关资源
      最近更新 更多