Using Vue.js (v3 Beta) with Ionic Components & Capacitor Plugins
Tags: vue, javascript, ionic, tutorial
Published: 2020-09-07 14:50:54

####[See My Upcoming Book On Ionic & Vue JS](http://bit.ly/ionicvuebook)
---
>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](https://capacitor.ionicframework.com/) web/mobile application using the latest version [Ionic Framework Vue.js](https://ionicframework.com/docs) Components and [Vue.js, version three](https://v3.vuejs.org/) which is still in beta.

> ####_[Vue3 Ionic Video Playlist Available Here](https://www.youtube.com/playlist?list=PL2PY2-9rsgl2uKW0DB5FJ-YRCAG-JuSWN)_

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.

### The Video...
{% youtube ghPWh45h-SM %}

### Whats Covered...
>created project using [latest cli from Ionic](https://www.npmjs.com/package/@ionic/cli/v/6.11.8) using `ionic start --tag vue-beta`

- Using VueJS for basic application with Ionic Components
- Using the [Capacitor Geolocation Plugin](https://capacitorjs.com/docs/apis/geolocation) in mobile application and in PWA
- Using the [Capacitor Camera Plugin](https://capacitor.ionicframework.com/docs/apis/camera) in mobile application and in PWA

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ccz0og9835ggch659ucw.png)

####[🔆Click Here for Ionic Framework ReactJS and VueJS Tips/Tutorials?🔆](https://buff.ly/35bAMIf) 

##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`
```vuejs
<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
```javascript
// 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.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9zsz4q4ylqb7goikxfq8.png)

- 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
```vuejs
<!-- in template -->
<div class="ion-padding">
   <img :src="imageUrl ? imageUrl : null" />
</div>
```
```javascript
// 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](https://capacitor.ionicframework.com/docs/apis/camera) in the application.
```javascript
// 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
```javascript
//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


## Source Code
{% github aaronksaunders/capacitor-vue3-ionicv5-app %}

####[🔆Click Here for Ionic Framework ReactJS and VueJS Tips/Tutorials?🔆](https://buff.ly/35bAMIf)