Vue.js course ยท Module 11: Pinia
Pinia Basics
In this lesson5
Welcome to the NOVA LAB Central Database! Year 2087 - this is where we store all critical data from the Mars mission. Just as the base's main computer stores information about systems, crew, and resources, Pinia stores the global state of a Vue application.
What is Pinia?
Pinia is the official state management library for Vue 3. It is the successor to Vuex and offers much simpler syntax. Think of it as the central computer of NOVA LAB - a single control point from which all station modules retrieve data.
Main advantages of Pinia:
- Simpler syntax than Vuex (no mutations!)
- Full TypeScript support
- Great integration with Vue DevTools
- Modular architecture - multiple independent stores
Installation and Setup
To start Pinia, we need two steps - install the package and connect it to the application:
npm install pinia// main.js
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
const pinia = createPinia()
const app = createApp(App)
app.use(pinia)
app.mount('#app')The createPinia() function creates a Pinia instance, and app.use(pinia) registers it as a Vue plugin. From this point on, all stores are available throughout the application.
Defining a Store - defineStore
A store is created using defineStore. It takes two arguments: a unique store name and a setup function (Composition API style):
// stores/mission.js
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
export const useMissionStore = defineStore('mission', () => {
// State - reactive data
const missionName = ref('Mars Exploration Alpha')
const status = ref('active')
const crewCount = ref(6)
// Getters - computed values
const statusLabel = computed(() =>
status.value === 'active' ? 'ACTIVE' : 'INACTIVE'
)
// Actions - functions that modify state
function updateStatus(newStatus) {
status.value = newStatus
}
// Return everything that should be accessible
return {
missionName,
status,
crewCount,
statusLabel,
updateStatus
}
})Naming convention: useXxxStore - prefix use and suffix Store.
Using the Store in a Component
<script setup>
import { useMissionStore } from '@/stores/mission'
const missionStore = useMissionStore()
// Access state
console.log(missionStore.missionName)
// Call an action
missionStore.updateStatus('completed')
</script>
<template>
<div>
<h1>{{ missionStore.missionName }}</h1>
<p>Status: {{ missionStore.statusLabel }}</p>
<button @click="missionStore.updateStatus('paused')">
Pause Mission
</button>
</div>
</template>storeToRefs - Reactive Destructuring
Regular destructuring loses reactivity. Use storeToRefs for state and getters:
<script setup>
import { useMissionStore } from '@/stores/mission'
import { storeToRefs } from 'pinia'
const store = useMissionStore()
// State and getters - storeToRefs (preserves reactivity)
const { missionName, status, statusLabel } = storeToRefs(store)
// Actions - regular destructuring (they're functions, not refs)
const { updateStatus } = store
</script>Code for this lesson: App.vue
<script setup>
import { ref, computed } from 'vue'
// Pinia store simulation (Composition API style)
// In a real app: import { defineStore } from 'pinia'
// State
const missionName = ref('Mars Exploration Alpha')
const status = ref('active')
const crewCount = ref(6)
const fuelLevel = ref(87)
// Getters
const statusLabel = computed(() =>
status.value === 'active' ? 'ACTIVE' : 'INACTIVE'
)
const fuelStatus = computed(() => {
if (fuelLevel.value > 70) return 'Optimal'
if (fuelLevel.value > 30) return 'Moderate'
return 'Critical'
})
// Actions
function toggleStatus() {
status.value = status.value === 'active' ? 'paused' : 'active'
}
function consumeFuel(amount) {
fuelLevel.value = Math.max(0, fuelLevel.value - amount)
}
function refuel() {
fuelLevel.value = 100
}
</script>
<template>
<div class="nova-lab">
<h1>NOVA LAB - Pinia Basics</h1>
<div class="panel">
<h2>Mission Store</h2>
<div class="stat">
<span class="label">Mission:</span>
<span>{{ missionName }}</span>
</div>
<div class="stat">
<span class="label">Status:</span>
<span :class="status">{{ statusLabel }}</span>
</div>
<div class="stat">
<span class="label">Crew:</span>
<span>{{ crewCount }} members</span>
</div>
<div class="stat">
<span class="label">Fuel:</span>
<div class="fuel-bar">
<div class="fuel-fill" :style="{ width: fuelLevel + '%' }" :class="fuelStatus.toLowerCase()"></div>
</div>
<span>{{ fuelLevel }}% ({{ fuelStatus }})</span>
</div>
<div class="actions">
<button @click="toggleStatus" class="btn">
{{ status === 'active' ? 'Pause' : 'Resume' }}
</button>
<button @click="consumeFuel(10)" class="btn btn-warn">
Consume Fuel (-10)
</button>
<button @click="refuel" class="btn btn-success">
Refuel
</button>
</div>
</div>
</div>
</template>
<style scoped>
.nova-lab {
background: #0a0e27;
color: #00ff88;
min-height: 100vh;
padding: 20px;
font-family: monospace;
}
h1 { color: #00ff88; margin-bottom: 20px; text-align: center; }
.panel {
background: rgba(0, 180, 216, 0.1);
border: 2px solid #00b4d8;
padding: 20px;
max-width: 500px;
margin: 0 auto;
}
h2 { color: #00b4d8; margin-bottom: 15px; }
.stat {
display: flex;
align-items: center;
gap: 10px;
margin: 10px 0;
flex-wrap: wrap;
}
.label { color: #00b4d8; min-width: 80px; }
.active { color: #00ff88; font-weight: bold; }
.paused { color: #ff9900; font-weight: bold; }
.fuel-bar {
width: 150px;
height: 16px;
background: #1a1e3f;
border: 1px solid #00b4d8;
}
.fuel-fill {
height: 100%;
transition: width 0.3s;
}
.fuel-fill.optimal { background: #00ff88; }
.fuel-fill.moderate { background: #ff9900; }
.fuel-fill.critical { background: #ff4444; }
.actions {
display: flex;
gap: 8px;
margin-top: 15px;
flex-wrap: wrap;
}
.btn {
background: #00b4d8;
color: #0a0e27;
border: none;
padding: 8px 16px;
cursor: pointer;
font-family: monospace;
font-weight: bold;
}
.btn:hover { background: #00ff88; }
.btn-warn { background: #ff9900; }
.btn-warn:hover { background: #ffbb33; }
.btn-success { background: #00ff88; }
.btn-success:hover { background: #33ffaa; }
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is Pinia in the Vue 3 ecosystem?
2. How do you create a Pinia instance in main.js?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Create reactive state (missionName, status, crewCount) and a toggleStatus action.
- Click in order
Arrange the elements to correctly call defineStore
- Code editor
Use storeToRefs to destructure the state and call an action from the template.