Search code examples
typescriptionic2undefinedcordova-pluginstypescript-typings

Ionic 2: Use cordova plugin with Typescript that has its own namespace


I want to use the LibraryHelper plugin in my Ionic 2 app, but the docs for it defines its usage statically, e.g.:

LibraryHelper.saveVideoToLibrary(....)

NOT through:

window.plugins.LibraryHelper.saveVideoToLibrary(....

When I write the first line in my Typescript .ts file, I get the error:

Cannot find name 'LibraryHelper'

Q) How do I import the LibraryHelper plugin into my Ionic 2 app and be able to reference it without it - being undefined as above?


Solution

  • Make sure to install the plugin with the --save option

    ionic plugin add cordova-library-helper --save 
    

    You need to declare the global object LibraryHelper in your class after your imports.

    //imports
    declare var LibraryHelper:any;
    @Component({..})
    //..
    

    When you are using in your component, just make sure to wrap within

    this.platform.ready().then(()=>{
      LibraryHelper.saveVideoToLibrary(....)
    })