Ifch0o1
Ifch0o1

Reputation: 924

Use cordova plugins inside Ionic Vue.js Components

I see I can use any cordova plugin within ionic capacitor. But how I can use it inside vue component for example.

Just for example:

  1. I installed plugin cordova-plugin-prevent-screenshot-coffice with npm

npm i --save cordova-plugin-prevent-screenshot-coffice

  1. I trying to import foo from "cordova-plugin-prevent-screenshot-coffice", but it says cannot find that module.

Is there a special way to import plugins like the above example plugin (or any other plugin which is not in @ionic-native namespace)?

I need usage example for the ionic vue community bacause it's very hard to found any info how we can use it.

This not working for cordova-plugin-prevent-screenshot-coffice

import PLUGIN_X from "PLUGIN_X"

export default defineComponent({
  // ...
  mounted() {
    PLUGIN_X.doSomething()
  }
  // ...
}

Upvotes: 0

Views: 795

Answers (1)

jcesarmobile
jcesarmobile

Reputation: 53361

You don't have to import the plugin, the plugin is available on window object.

You can do something like this:

  // Enable
  (<any>window).plugins.preventscreenshot.enable((a) => this.successCallback(a), (b) => this.errorCallback(b));

  // Disable
  (<any>window).plugins.preventscreenshot.disable((a) => this.successCallback(a), (b) => this.errorCallback(b));

  successCallback(result) {
    console.log(result); // true - enabled, false - disabled
  }

  errorCallback(error) {
    console.log(error);
  }

or like this if using javascript instead of typescript

document.addEventListener("deviceready", onDeviceReady, false);
// Enable
function onDeviceReady() {
  window.plugins.preventscreenshot.enable(successCallback, errorCallback);
}
// Disable
function onDeviceReady() {
  window.plugins.preventscreenshot.disable(successCallback, errorCallback);
}

function successCallback(result) {
  console.log(result); // true - enabled, false - disabled
}

function errorCallback(error) {
  console.log(error);
}


document.addEventListener("onTookScreenshot",function(){
// Receive notification when screenshot is ready;
});

document.addEventListener("onGoingBackground",function(){
// Receive notification when control center or app going in background.
});

Upvotes: 1

Related Questions