Search code examples
androidangularcordova-plugins

Using cordova plugin in angular 4 typescript file without ionic


I want to create a mobile app using angular4. I have created an angular project using angular CLI now I want to use/implement cordova plugin in my angular project. How to do so without using IONIC framework?

For eg: I want to use cordova network information plugin to get the state of current network in my app.


Solution

  • You should install and integrate your angular project with cordova (it's doable without IONIC).

    I recomend to read these articles .

    1) https://www.becompany.ch/en/blog/2016/10/19/creating-apache-cordova-app-with-angular2 .

    2) https://www.becompany.ch/en/blog/2017/05/31/update-angular-cli-and-apache-cordova

    And use this repo as an example (cordova-integration branch):

    https://github.com/becompany/angular2-rss-reader-tutorial/tree/cordova-integration

    It's a bit old, but I'm using this approach for my Angular+Cordova projects even with Angular 5+.

    There you can add any existing plugins to Cordova app.

    UPDATE: To use some plugin in TS file you should declare that variable in the file as on example below:

    declare const PushNotifications;
    

    And check it is it existing and use:

    if (window['PushNotification']) {
      PushNotification.init(...);
    }
    

    Smth like that.