Blog>
Snippets

Integrating Vue 3 with Mobile Frameworks like Ionic

Create a sample integration of Vue 3 within Ionic Framework, illustrating the setup of a mobile project that uses Vue for its components.
import { createApp } from 'vue';
import { IonicVue } from '@ionic/vue';

// Make sure to import Ionic CSS
import '@ionic/vue/css/ionic.bundle.css';

import App from './App.vue';
import router from './router';

// Initialize Ionic with Vue 3
const app = createApp(App)
  .use(IonicVue)
  .use(router);
  
// Wait for the `deviceready` event to ensure plugins are loaded
document.addEventListener('deviceready', () => {
  app.mount('#app');
}, false);
This code snippet is the main entry point (e.g., main.js) which sets up Vue 3 to work with Ionic by importing IonicVue and mounting the Vue app once the device is ready.
import { createRouter, createWebHistory } from 'vue-router';
import HomePage from './views/HomePage.vue';

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: HomePage
    }
    // define other routes as needed
  ]
});

export default router;
This code snippet sets up the Vue router which is necessary for managing navigation within a single-page application in Vue. It also demonstrates defining a route path for the homepage component.
<template>
  <ion-page>
    <ion-header>
      <ion-toolbar>
        <ion-title>Home</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content :fullscreen="true">
      <p>Welcome to your Ionic App</p>
    </ion-content>
  </ion-page>
</template>

<script>
import { IonPage, IonHeader, IonToolbar, IonTitle, IonContent } from '@ionic/vue';
export default {
  components: {
    IonPage,
    IonHeader,
    IonToolbar,
    IonTitle,
    IonContent
  }
};
</script>
This code snippet is a Vue component that uses Ionic UI components. It demonstrates the usage of the ion-page, ion-header, ion-toolbar, ion-title, and ion-content components to create a basic Ionic page in Vue.