Auto Import

On-demand PrimeVue components with auto importing.

PrimeVue components need to be imported and configured individually. In the next section, we'll cleanup the code using auto imports.


import { createApp } from "vue";
import PrimeVue from "primevue/config";
import InputText from 'primevue/inputtext';
import Button from 'primevue/button';
import App from './App.vue'
const app = createApp(App);

app.use(PrimeVue);
app.component('InputText', InputText);
app.component('Button', Button);

The unplugin-vue-components library can automatically import and register PrimeVue components with the built-in resolver. Let's begin with installing the library.


npm i unplugin-vue-components -D

Next step would be adding the PrimeVueResolver at vite.config using the Components plugin.


import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite';
import {PrimeVueResolver} from 'unplugin-vue-components/resolvers';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    Components({
      resolvers: [
        PrimeVueResolver()
      ]
    })]
})

That's it, now the initialization code can be refactored as the following. For configuration like namespacing, visit the official documentation.


import { createApp } from "vue";
import PrimeVue from "primevue/config";
import App from './App.vue'
const app = createApp(App);

app.use(PrimeVue);

A complete example using a PrimeVue and unplugin is available at primevue-examples repository. You can also view this sample live at Stackblitz.