Mobile Development w/Vuejs and Ionic Capacitor Plugins

aaronksaunders

Aaron K Saunders

Posted on January 9, 2020

Mobile Development w/Vuejs and Ionic Capacitor Plugins

Photo by israel palacio on Unsplash

Overview

This sample/tutorial will walk through the integration of the following features in an Ionic Capacitor web/mobile application using Vuejs.

Capacitor Camera Plugin Integration 

There are a set of plugins that come as a default with Capacitor, the Camera and Geolocation are in that category. To access those plugins from @capacitor/core node module.

import {  
  Plugins,  
  CameraSource,  
  CameraResultType
} from "@capacitor/core";
const { Camera } = Plugins;
Enter fullscreen mode Exit fullscreen mode

Now to call the methods on the Camera

const image = await Camera.getPhoto({
  quality: 90,            
  allowEditing: true,            
  resultType: CameraResultType.DataUrl,            
  source: CameraSource.Prompt
});
Enter fullscreen mode Exit fullscreen mode

And for Geolocation

let location = await Geolocation.getCurrentPosition({
  enableHighAccuracy: true,        
  timeout: 30000      
});
Enter fullscreen mode Exit fullscreen mode

Get Camera Working In PWA/Website

Installed PWA Elements

npm install @ionic/pwa-elements
Enter fullscreen mode Exit fullscreen mode

Then opened up the main.js file in the vue project and make the following changes

import { defineCustomElements } from '@ionic/pwa-elements/loader'; // <== NEW

Vue.config.productionTip = false;

Vue.use(Ionic);
new Vue({
  router,
  render: h => h(App)
}).$mount("#app");

defineCustomElements(window);  // <== NEW

Enter fullscreen mode Exit fullscreen mode

and then the magic happened, Ionic will now use the pwa-element to access the web camera instead of looking for the device camera.

Adding A Non Capacitor Plugin

I am testing with the Barcode Scanner Plugin, you will need to install the plugin using npm

npm install phonegap-plugin-barcodescanner
Enter fullscreen mode Exit fullscreen mode

and then in the source code you get access to the plugin off of the window object. In your code, you can also check the window object for cordova to make sure the user doesn't try and load the barcode scanner in the browser.

window.cordova.plugins.barcodeScanner.scan(
  function(result) { /* success */ },
  function(error) { /* error */ },
  { /* settings */ }
  );
Enter fullscreen mode Exit fullscreen mode

GitHub logo aaronksaunders / capacitor-vue-ionicv4-app

sample app using capacitor vuejs and ionicv4 components






Video Playlist On Vue and Vue Composition API

Additional Ionic Framework VueJS Samples

💖 💪 🙅 🚩
aaronksaunders
Aaron K Saunders

Posted on January 9, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related