🍍
直观易用
直观的 API,让状态管理变得简单明了。无需复杂的样板代码,专注于业务逻辑。
npm install pinia
yarn add pinia
pnpm add pinia
// main.js
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
const app = createApp(App)
app.use(createPinia())
app.mount('#app')
// stores/counter.js
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('counter', {
state: () => ({
count: 0
}),
getters: {
doubleCount: (state) => state.count * 2
},
actions: {
increment() {
this.count++
}
}
})
<!-- 在组件中使用 -->
<template>
<div>
<p>计数: {{ counter.count }}</p>
<p>双倍计数: {{ counter.doubleCount }}</p>
<button @click="counter.increment()">增加</button>
</div>
</template>
<script setup>
import { useCounterStore } from '@/stores/counter'
const counter = useCounterStore()
</script>
Pinia 是 Vue.js 的官方状态管理库,它提供了更简单、更直观的 API,同时保持了强大的功能:
准备好开始了吗?查看我们的 快速开始指南 或了解 Pinia 的核心概念。