Using Vue v3 Beta with Ionic Components & Capacitor Plugins

Update from original post Jan 2019 which was based on Vue.js version 2 https://dev.to/aaronksaunders/mobile-development-w-vuejs-and-ionic-capacitor-plugins-5fb5

Overview

This sample/tutorial will walk through the integration of the following features in an Ionic Capacitor web/mobile application using the latest version Ionic Framework Vue.js Components and Vue.js, version three which is still in beta.

I am also using the latest beta release of the Ionic integration with Vue. I am using typescript in the example and relying heavily on the new pattern of composition when creating single file components.

created project using latest cli from Ionic using ionic start --tag vue-beta

🔆 Click Here for Ionic Framework ReactJS and VueJS Tips/Tutorials?

Basic Page Structure

A bit different than in the past, please see comments in the code below and the important points highlighted at end of section.

Main thing to notice, there is no data, methods, etc sections any more, it is all handled in the setup

<script lang="ts">
// import components from ionic
import {
IonContent,
IonHeader,
IonPage,
IonTitle,
IonToolbar,
} from "@ionic/vue";
// import capacitor plugins
import { Plugins, CameraSource, CameraResultType } from "@capacitor/core";
const { Camera } = Plugins;
// import from vue
import { defineComponent, ref } from "vue";
// import to get access to the router
import { useRouter } from "vue-router";
// define the component
export default defineComponent({
// provide the name
name: "Home",
// provide the list of components being used in the
// template
components: {
IonContent,
IonHeader,
IonPage,
IonTitle,
IonToolbar,
},
// setup function, It is called after props resolution, when
// instance of the component is created.
setup() {
const imageUrl = ref<string | null>();
// get access to router
const router = useRouter();
// functions
const nextPage = () => { };
const takePicture = async () => { };
// return the props and functions to component
// so they are accessible in the template
return {
takePicture,
imageUrl,
nextPage,
};
},
});
</script>

Important Points

  • there is no access to this in the setup() function
// old way
this.$router.push("/next-page");
// new way
const router = useRouter();
router.push("/next-page");
  • everything that is returned from the setup() function is available to use in the template.
  • values/properties defined using ref are accessible without a need to unwrap in template but you must use imageUrl.value when accessing in a function
<!-- in template -->
<div class="ion-padding">
<img :src="imageUrl ? imageUrl : null" />
</div>
// in function
console.log(imageUrl.value);

Capacitor Plugins Support

Plugins are imported and utilized the same as they were in the previous version. The one difference you will notice is how the data properties are accessed.

Here is the code for using the Camera plugin in the application.

// import
import { Plugins, CameraSource, CameraResultType } from "@capacitor/core";
const { Camera } = Plugins;
// code inside of `setup()`
const takePicture = async () => {
try {
const image = await Camera.getPhoto({
quality: 90,
allowEditing: true,
resultType: CameraResultType.DataUrl,
source: CameraSource.Prompt,
});
console.log("image", image);
// image.base64_data will contain the base64 encoded
// result as a JPEG, with the data-uri prefix added
// unwrap to set the `value`
imageUrl.value = image.dataUrl;
// can be set to the src of an image now
console.log(image);
} catch (e) {
console.log("error", e);
}
};

Capacitor PWA Support

Same as before just be sure to include the library and call defineCustomElements(window); after component is mounted

//main.ts
import { defineCustomElements } from '@ionic/pwa-elements/loader';
const app = createApp(App)
.use(IonicVue)
.use(router);
router.isReady().then(() => {
app.mount('#app');
defineCustomElements(window);
});

CLI Integration

You can now use the same ionic commands for building and running your application since vue support is integrated in the CLI

Video

Source Code

🔆 Click Here for Ionic Framework ReactJS and VueJS Tips/Tutorials?

DC based software agency utilizing #Javascript, #Typescript, #HTML5, #Ionicframework, #NodeJS, #Firebase to build web & mobile solutions. https://buff.ly/300Zru

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store