Guide on implementing a cordova plugin in a TypeScript cordova application

In my Cordova project, which utilizes Angular and Typescript, I am facing issues with implementing the juspay-ec-sdk-plugin for Cordova. I have explored possible solutions on StackOverflow related to integrating Cordova plugin in an Angular 4 Typescript application as well as resolving Cordova plugin errors in Typescript. However, none of them seem to work. Can anyone provide guidance on how to properly reference this plugin within my Typescript code? P.S.: I attempted to install ngCordova typings without any success.

Answer №1

In my primary app component, app.component.ts, I manage the device readiness and utilize a service to store the "cordova" functionalities.

Here is an example:

declare var cordova: any;  //<--declare "cordova"
declare var window:any;    //<--declare "window"

//An enum of events
export enum CordovaEvent {BackButton,Resume,Pause}

//Inject our "CordovaService" in the constructor 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 plugins...

    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

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking to receive detailed compiler error messages along with full imports in Visual Studio Code for TypeScript?

Currently, I am trying to edit a piece of typescript code in Visual Studio Code. However, I encountered a compiler error message that looks like this: The error states: Type 'import(\"c:/path/to/project/node_modules/@com.m...' is not assign ...

Leveraging an external TypeScript library in a TypeScript internal module

Imagine you find yourself in a situation where you need to utilize a typescript/node library within an internal module that is spanned across multiple .ts files. ApiRepositoryHelper.ts import * as requestPromise from "request-promise"; module ApiHelp ...

Startling error: Our node server.js running into an unexpected token

I'm attempting to follow the Angular Universal quickstart, but I encountered an error when running "node server.js". Emily's-MBP:vepo Emily$ node server.js /Users/Emily/Development/vepo/server.js:3 import 'angular2-universal/polyfills&apos ...

What is the best way to adjust the Material Drawer width in Reactjs to match the width of its children?

Currently, I am utilizing the Material-ui Drawer component to toggle the display of content on the right side of the screen. The functionality I am aiming for is that when the Drawer opens, it will shrink the existing content on the right without any overl ...

Tips for avoiding the transmission of className and style attributes to React components

Hey there! I'm working on a custom button component that needs to accept all ButtonHTMLAttributes except for className and style to avoid any conflicts with styling. I'm using TypeScript with React, but I've run into some issues trying to ac ...

Ways to decrease the node_module size within an Angular application and how to efficiently load Node_Modules directly from the index.html or the root

Looking to decrease the size of node_module (currently 605MB) and load Node_Modules from index.html or application root. Using Angular 5 with node 8.9.4 Various solutions have been attempted but without the desired outcome, npm install --production Git ...

Paginator for Angular Material Cards

This section contains the TypeScript file for a component import { MatTableDataSource, MatPaginator } from "@angular/material"; import { FoodService } from "./food.service"; import { Food } from "./food"; @Component({ selec ...

Displaying a div component in React and Typescript upon clicking an element

I've been working on a to-do list project using React and TypeScript. In order to display my completed tasks, I have added a "done" button to the DOM that triggers a function when clicked. Initially, I attempted to use a useState hook in the function ...

The file size of clr-ui-dark.min.css seems unexpectedly large, despite attempts to optimize the bundles

Has anyone else noticed a similar issue in their projects, or could it be that I made a mistake? It appears to me that the dark styling comprises roughly 33% (according to webpack-bundle-analyzer) of my app's total size. ...

Anticipating the resolution of promises and observables in Angular 2

Within my accountService module, there is a dialog prompt that requests the user's username and password, returning a promise. If the user clicks on close instead of dismissing the dialog box and the validators require the input data before allowing t ...

Is there a way to integrate TypeScript into the CDN version of Vue?

For specific areas of my project, I am utilizing the Vue CDN. I would like to incorporate Typescript support for these sections as well. However, our technical stack limitations prevent us from using Vue CLI. Is there a method to import Vue.js into a bas ...

When using Jest + Enzyme to test a stateful class component, encountering problems with Material-UI's withStyles functionality is a common issue

I have a React + TypeScript application with server-side rendering (SSR). I am using Material-UI and following the SSR instructions provided here. Most of my components are stateful, and I test them using Jest + Enzyme. Below is an example of one such com ...

When working with Angular 5, the question arises: how and where to handle type conversion between form field values (typically strings) and model properties (such

As a newcomer to Angular, I am struggling with converting types between form field values (which are always strings) and typed model properties. In the following component, my goal is to double a number inputted by the user. The result will be displayed i ...

TS1057: It is required that an async function or method has a return type that can be awaited

There was a recent Github issue reported on March 28th regarding async arrow functions generating faulty code when targeting ES5, resulting in the error message: TS1057: An async function or method must have a valid awaitable return type You can find t ...

Include a character in a tube using Angular

Hey everyone, I have a pipe that currently returns each word with the first letter uppercase and the rest lowercase. It also removes any non-English characters from the value. I'm trying to figure out how to add the ':' character so it will ...

Disregard earlier callback outcome if there has been a change in the state since then

I am facing an issue with my page that displays a list of countries retrieved from an external library. When I click on a country, it should show me all the cities in that specific country. Each country object has a provided method to fetch the list of c ...

Angular 8 is facing an issue where classes defined dynamically in the 'host' property of a directive are not being properly applied to the parent template

In my Angular 8 application, I am working on implementing sorting using the ng-bootstrap table. I referred to the example available at . In the sample, when I hover over the table header, it changes to a hand pointer with a highlighted class applied as sho ...

Angular 14 encountered an unexpected issue: There was an unhandled exception with the script file node_modules/tinymce/themes/modern/theme.min.js missing

After attempting to run ng serve, an error message appears: ⠋ Generating browser application bundles (phase: setup) ...An unhandled exception occurred: Script file node_modules/tinymce/themes/modern/theme.min.js does not exist. ⠋ Generating browser ap ...

Definition files (.d.ts) for JavaScript modules

I'm currently working on creating Typescript typings for the link2aws package in order to incorporate it into my Angular project. Despite generating a .d.ts file, I am still encountering the following error message: TypeError: (new link2aws__WEBPACK_I ...

How can I add a clear button to the datepicker in Ionic version

Trying to implement a clear button to reset the value selected with the date picker. The code snippet below has been added to my date picker component along with corresponding TypeScript. customPickerOptionFrom = { buttons: [{ text: 'Clea ...