Vue.js PrimeVue
Introduction
PrimeVue is a rich and comprehensive UI component library for Vue.js applications. Built on top of the popular PrimeFaces design, it offers a collection of over 90 flexible, feature-rich components that can accelerate your Vue development process significantly.
As a beginner learning Vue.js, incorporating a well-established UI library like PrimeVue will help you build professional-looking applications without having to create UI components from scratch. PrimeVue is specifically designed for Vue 3, but also supports Vue 2.
In this guide, we'll explore:
- Setting up PrimeVue in your Vue.js project
- Core PrimeVue components and their usage
- Styling and theming options
- Building a practical application with PrimeVue
Getting Started with PrimeVue
Installation
First, you need to install PrimeVue in your Vue.js project:
# Using npm
npm install primevue
# Using yarn
yarn add primevue
You'll also need the PrimeVue styles and icons:
npm install primeicons
Basic Setup
Once installed, you need to configure PrimeVue in your Vue application.
For Vue 3 applications, you can set it up in your main.js
file:
import { createApp } from 'vue';
import App from './App.vue';
import PrimeVue from 'primevue/config';
import 'primevue/resources/themes/lara-light-blue/theme.css'; // theme
import 'primevue/resources/primevue.min.css'; // core css
import 'primeicons/primeicons.css'; // icons
const app = createApp(App);
app.use(PrimeVue);
app.mount('#app');
Importing Individual Components
PrimeVue allows you to use components individually. This approach helps to keep your bundle size small by including only the components you need:
<template>
<div>
<h2>My First PrimeVue Component</h2>
<Button label="Click Me" @click="showMessage" />
<Toast />
</div>
</template>
<script>
import Button from 'primevue/button';
import Toast from 'primevue/toast';
import { useToast } from 'primevue/usetoast';
export default {
components: {
Button,
Toast
},
setup() {
const toast = useToast();
const showMessage = () => {
toast.add({
severity: 'success',
summary: 'Success',
detail: 'You clicked the button!',
life: 3000
});
};
return {
showMessage
};
}
}
</script>
Core PrimeVue Components
PrimeVue offers a wide range of components for various UI needs. Let's explore some of the most commonly used ones:
Form Components
InputText
The most basic form component is InputText, which enhances the standard HTML input element:
<template>
<div class="p-field">
<label for="username">Username</label>
<InputText id="username" v-model="username" aria-describedby="username-help" />
<small id="username-help">Enter your username to login.</small>
</div>
</template>
<script>
import InputText from 'primevue/inputtext';
export default {
components: {
InputText
},
data() {
return {
username: ''
};
}
}
</script>
Dropdown
Dropdown provides a way to select an item from a collection:
<template>
<div class="p-field">
<label for="city">City</label>
<Dropdown id="city" v-model="selectedCity" :options="cities" optionLabel="name"
placeholder="Select a City" />
</div>
</template>
<script>
import Dropdown from 'primevue/dropdown';
export default {
components: {
Dropdown
},
data() {
return {
selectedCity: null,
cities: [
{ name: 'New York', code: 'NY' },
{ name: 'London', code: 'LDN' },
{ name: 'Paris', code: 'PRS' },
{ name: 'Tokyo', code: 'TK' }
]
};
}
}
</script>
Data Display Components
DataTable
DataTable is one of the most powerful PrimeVue components. It allows you to display data in a tabular format with features like sorting, filtering, and pagination:
<template>
<DataTable :value="products" :paginator="true" :rows="5"
tableStyle="min-width: 50rem">
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
<Column field="category" header="Category"></Column>
<Column field="quantity" header="Quantity"></Column>
<Column field="price" header="Price">
<template #body="slotProps">
${{ slotProps.data.price.toFixed(2) }}
</template>
</Column>
</DataTable>
</template>
<script>
import DataTable from 'primevue/datatable';
import Column from 'primevue/column';
export default {
components: {
DataTable,
Column
},
data() {
return {
products: [
{ code: '1001', name: 'Product A', category: 'Electronics', quantity: 10, price: 299.99 },
{ code: '1002', name: 'Product B', category: 'Clothing', quantity: 25, price: 49.99 },
{ code: '1003', name: 'Product C', category: 'Home', quantity: 8, price: 199.99 },
{ code: '1004', name: 'Product D', category: 'Electronics', quantity: 15, price: 399.99 },
{ code: '1005', name: 'Product E', category: 'Clothing', quantity: 30, price: 29.99 },
{ code: '1006', name: 'Product F', category: 'Home', quantity: 12, price: 129.99 },
]
};
}
}
</script>
Card
Card is a flexible container component:
<template>
<Card>
<template #header>
<img alt="user header" src="https://primefaces.org/cdn/primevue/images/usercard.png" />
</template>
<template #title>
Advanced Card
</template>
<template #content>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Inventore sed consequuntur error repudiandae numquam.</p>
</template>
<template #footer>
<Button icon="pi pi-check" label="Save" />
<Button icon="pi pi-times" label="Cancel" class="p-button-secondary" style="margin-left: .5em" />
</template>
</Card>
</template>
<script>
import Card from 'primevue/card';
import Button from 'primevue/button';
export default {
components: {
Card,
Button
}
}
</script>