Ionic 2 and custom plugin


#1

Hi all,

I’m trying to use a custom plugin in typescript but I don’t know how to.
I followed the usual tutorial
https://cordova.apache.org/docs/en/latest/guide/hybrid/plugins/index.html
and created a plugin manually just for android for now
then I used plugman to install it. After opening Android Studio I can see that everything is linked up together.

However how do you use the plugin in a ts file?
How do you use the import statement to import a custom plugin?

Nothing seems to be attached to window.plugins or window.cordova.plugins.

VS Code does not seem to know about the new plugin either. For instance the Device plugin has some interfaces defined in /node-modules/ionic-native. Do I have to do that as well? Currently I cannot access any plugins including the one I created manually.


#2

Could you explain a bit more?
You’re saying you have a custom cordova plugin that is written in Typescript?
If that’s the case, then it would not work with cordova.

Plugins need a JS file, not TS.


#3

My bad.
I just realised that the “clobbers” node in my plugin.xml was incorrect. I did a copy paste from the Device plugin and forgot to change the target attribute. So I ended up having 2 plugins with the “device” target. It must have confused the hell out of cordova and that’s why I couldn’t use the device plugin either.

Also I thought that in the ts file we were supposed to import the plugin using something like:
import {MyPlugin} from ‘somewhere’

but apparently you don’t need to. The plugin is available from the window object.

Problem solved.

Thanks.


#4

Yes the plugin is available on the window object.

The typescript files for the plugins serve only two purposes.

  1. Code hints and type definitions for your editor.
  2. Make cordova plugins play well with angular and rxjs.

#5

Hello @mhartington, @ngarel

I passed something similar, correctly install the plugin with the plugman tool, but then when you launch the app with “ionic run android” the plugin installed disappears from android project and is not accessible from "windows. <cobler_id>. <name_plugin>. < name_function> "

what am I doing wrong

Regards


Accessing external cordova plugin
#6

You need to check the clobber in the plugin.xml.

So for example, take the keyboard plug in.

Here we would target it through window.cordova.plugin.Keyboard namespae, since thats the target.


Reference custom plugins with ionic 2 (speech to text)