Vue CLI Pinia Plugin
The `vue-cli-plugin-pinia` package integrates Pinia, the official state management library for Vue.js, into projects scaffolded by Vue CLI. This plugin streamlines the setup process for Pinia by automatically configuring it within an existing Vue CLI project, including necessary dependencies and boilerplate code for store initialization. The current stable version, 0.2.4, reflects ongoing, albeit irregular, maintenance primarily focused on bumping Pinia dependencies and ensuring compatibility with various Vue CLI versions. While it offers a convenient, automated approach for adding Pinia, its utility is significantly impacted by the fact that Vue CLI itself is now in maintenance mode. New Vue projects are strongly advised to use `create-vue` to scaffold Vite-based projects, which offer a more modern and performant development experience. This plugin's primary role is therefore to serve existing Vue CLI applications that require Pinia integration, rather than being a solution for new development.
Common errors
-
command not found: vue
cause Vue CLI is not installed globally or not in your system's PATH.fixInstall Vue CLI globally: `npm install -g @vue/cli` or `yarn global add @vue/cli`. -
Error: The plugin `vue-cli-plugin-pinia` has already been applied.
cause You are trying to add the plugin to a project where it has already been installed.fixThis message indicates the plugin is already set up. No action is required unless you intended to re-install for an upgrade (which is usually handled by `npm update` or `yarn upgrade`). -
Module not found: Error: Can't resolve 'pinia'
cause Pinia dependency might not be correctly installed or its module resolution is failing after adding the plugin.fixRun `npm install` or `yarn install` to ensure all dependencies are present. If the issue persists, manually install Pinia: `npm install pinia` or `yarn add pinia`.
Warnings
- breaking Vue CLI itself is in maintenance mode. For new projects, the official recommendation is to use `create-vue` which scaffolds Vite-based applications. This plugin is primarily for existing Vue CLI projects.
- gotcha The plugin automatically injects the `@vue/composition-api` plugin only for Vue 2 projects with versions less than 2.7.0. If you are using Vue 2.7.0+, the Composition API is built-in and the injection is skipped. Ensure your Vue 2 setup aligns with this behavior.
- gotcha This plugin primarily handles the initial setup of Pinia. Subsequent updates to Pinia itself or its API may introduce breaking changes that are independent of the plugin but will affect your application.
Install
-
npm install vue-cli-plugin-pinia -
yarn add vue-cli-plugin-pinia -
pnpm add vue-cli-plugin-pinia
Imports
- createPinia
const { createPinia } = require('pinia')import { createPinia } from 'pinia' - defineStore
import defineStore from 'pinia'
import { defineStore } from 'pinia' - storeToRefs
import { toRefs } from 'vue'import { storeToRefs } from 'pinia'
Quickstart
vue add vue-cli-plugin-pinia
// src/main.ts (or main.js)
import { createApp } from 'vue';
import { createPinia } from 'pinia';
import App from './App.vue';
const app = createApp(App);
const pinia = createPinia();
app.use(pinia);
app.mount('#app');
// src/stores/counter.ts (example store)
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', {
state: () => ({
count: 0,
name: 'MyCounter',
}),
getters: {
doubleCount: (state) => state.count * 2,
},
actions: {
increment() {
this.count++;
},
},
});
// src/components/CounterDisplay.vue (example usage in a component)
<template>
<div>
<p>Count: {{ counter.count }}</p>
<p>Double Count: {{ counter.doubleCount }}</p>
<button @click="counter.increment()">Increment</button>
</div>
</template>
<script setup lang="ts">
import { useCounterStore } from '../stores/counter';
// import { storeToRefs } from 'pinia'; // Use storeToRefs if you need to destructure state properties reactively
const counter = useCounterStore();
</script>