ghot
ghot

Reputation: 11

use plugins non ionic-native in Ionic 2

I'm using a cordova plugin from github, not available in ionic native.

I have problem. How to use non ionic-native plugins in Ionic 2 ?

I try :

declare var myPlugin: any;

myPlugin.doSomething(...)

and other forum solutions

but I have error :

ReferenceError: myPlugin is not defined
at new HomePage (http://localhost:8100/build/main.js:18762:9)

My config :

Ionic Framework: ^2.0.0-rc.3
Ionic Native: ^2.2.3
Ionic App Scripts: 1.0.0
Angular Core: 2.2.1
Angular Compiler CLI: 2.2.1
Node: 6.9.5
OS Platform: Windows 10
Navigator Platform: Win32
cordova 6.5.0

Same problem : Working with non ionic-native plugins in Ionic 2

Upvotes: 0

Views: 1055

Answers (3)

Kishan Bharda
Kishan Bharda

Reputation: 5700

Please try like this :

First import plugin or library as :

import plugin from 'plugin-name' ;

And then use :

plugin.functionName();

Upvotes: 0

Suraj Rao
Suraj Rao

Reputation: 29635

Your problem is that in plugin.xml clobbers is set as window.plugins.myplugin. This makes window the global object and you have to declare:

declare var window: any;

and functions called as:

window.plugins.myPlugin.pluginInitialize()

or if this is your custom made plugin, you could make clobbers object as myPlugin

Upvotes: 1

Purus
Purus

Reputation: 5819

Few things to look:

  • Verify if you plugin entry is present in the plugin.xml and package.json files? If not you might need to add an entry as the below example.
  • Use the plugin only when the platform is ready.
  • Test your app in real device.
  • Use "ionic add" instead "cordova add" to add new plugins.

Refer the below thread which might be of some help on editing your plugin.xml file.

https://forum.ionicframework.com/t/how-to-use-non-native-plugins-on-ionic-2/63936/17

Upvotes: 1

Related Questions