How to deal with cordova plugin when developing in browser?

前端 未结 1 650
情话喂你
情话喂你 2021-01-29 01:22

I\'m developing an application Ionic, Angular and Cordova and I\'m looking for the best way to call Cordova plugins in Javascript while developing in the browser. What is the be

相关标签:
1条回答
  • 2021-01-29 01:44

    As cordova.js is only available when you are running application in device or emulator. So there is no way that you can use cordova plugins in browsers.As you need to put a check if(navigator.globalization) on every plugin call for testing on browsers, so i will suggest you to make your own wrapper or functions above these calls, some sort of global function which will call these plugins functions. Here is a sample with plain javascript.

    function MyGlobalizationService(){
    };
    
    MyGlobalizationService.prototype.getPreferredLanguage = function(onSuccess, onError){
     if(navigator.globalization) {
         navigator.globalization.getPreferredLanguage(onSuccess, onError);
     }
    }
    
    window.myGlobalizationService = new MyGlobalizationService();
    

    And then use window.myGlobalizationService functions anywhere in your application.

    0 讨论(0)
提交回复
热议问题