Why is it necessary to import Ionic components?

Looking at Quasar, Vuetify, and Nuxt, Ionic is the only framework that requires you to import the tags/components that you are going to use in a view. Why is that, and is there a way of auto-importing the components like other frameworks do? The Ionic way requires quite a lot of additional typing. When you add or remove tags in your view, you have to remember to add/remove them in three places (!) in your view: In the template, in the import, and in the components line. That makes the whole thing a bit inflexible. Here’s an example:

Ionic:

<template>
  <ion-button>Yes</ion-button>
</template>

<script>
import { IonButton } from '@ionic/vue';

export default {
  components: {
    IonButton
  }
});
</script>

Vuetify:

<template>
  <v-btn>Yes</v-btn>
</template>

Quasar:

<template>
  <q-btn label="Yes" />
</template>

Nuxt:

<template>
  <NuxtLink to="/">Home</NuxtLink>
</template>

The Ionic Vue Quickstart Guide goes over this: https://ionicframework.com/docs/vue/quickstart#optimizing-your-build

Here is the relevant text:

By default, Ionic Framework components are registered locally. With local registration, these components are imported and provided to each Vue component you want to use them in. This is the recommended approach as it allows lazy loading and treeshaking to work properly with Ionic Framework components.

If you prefer not to use local component registration, you can register the components globally once and then use it just as you would in Vuetify or Quasar:

import { IonContent, IonicVue, IonPage } from '@ionic/vue';

const app = createApp(App)
  .use(IonicVue)
  .use(router);

app.component('ion-content', IonContent);
app.component('ion-page', IonPage);
3 Likes