如何在 typescript cordova 项目中使用 cordova 插件?

2024-01-02

我有一个 cordova +angular+ typescript 项目,在其中我尝试使用 Cordova 的 juspay-ec-sdk-plugin 。我已经尝试过以下解决方案Angular 4 Typescript 应用程序中的 Cordova 插件 https://stackoverflow.com/questions/43662664/cordova-plugin-in-angular-4-typescript-application and cordova 插件无法在打字稿中解析 https://stackoverflow.com/questions/51424747/cordova-plugin-cannot-resolve-in-typescript但没有成功。如何在我的打字稿代码中引用这个插件? PS:我尝试安装 ngCordova types 但没有成功。


我喜欢在我的主要组件 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);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 typescript cordova 项目中使用 cordova 插件? 的相关文章

随机推荐