Vue CLI Pinia Plugin

0.2.4 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to add the plugin to a Vue CLI project and then create and use a basic Pinia store.

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>

view raw JSON →