ionic 2 error cordova not available

爷,独闯天下 提交于 2019-11-27 13:29:10

If you want the plugin to work for the browser you should add platform browser and run it:

ionic cordova platform add browser

and run it:

ionic cordova run browser

instead of ionic serve.

This error usually occurs when you're running the app in chrome using ionic serve which is normal as in the browser cordova native components are not there but also occur on emulator and devices when an ionic native plugin that you're using was nod added, even if you have added the ionic plugin for it.

For instance if you are using native Toast

then you need to add proper ionic dependencies:

ionic plugin add cordova-plugin-x-toast --save

but you also need to add cordova dependencies:

cordova plugin add cordova-plugin-x-toast --save

If you forget to add the later cordova plugin you'll get an error like:

Runtime Error Uncaught(in promise): cordova_not_available

Which can be tricky to find the cause.

Once you have added ionic and cordova dependencies you should be able to use it.

Make sure you import it:

import { Toast } from 'ionic-native';

inject Platform in constructor:

constructor(public navCtrl: NavController, private platform: Platform) {...

then use the native item:

this.platform.ready().then(() =>
      Toast.show("Successfull", '5000', 'center')
        .subscribe(
        toast => {
          console.log(toast);
        }
      ));
DeepFreez

Using ionic serve disables all the cordova plugins, because it is not running on a device.

Rather use ionic cordova run android This will start an android emulator that should allow all the cordova plugins to function

I have also come across the second approach, but the syntax then has to be windows['plugins'].googleplus.login(...)

Sometimes using ionic cordova run browser is not the best option, since it takes a long time for it to compile your changes.

In my case, what was causing the issue was the FCM plugin. It cannot run on the browser, if I use ionic serve. Since my code was inside app.component.ts, it was easy for me to get around the problem. I simply used the following line:

  if (platform.is('cordova')) 
     {  this.fcmx.onTokenRefresh().subscribe(token => { 
     this.pushNoti.storeNewToken(token); }); }

Using platform.is('cordova'), you can prevent the code causing trouble to run on the browser.

Simply Run after attaching android device

ionic cordova run android

make sure you correct google map key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!