如何在 Vue 应用程序中使用 Pinia 状态管理库?安装 Pinia创建 Store 模块使用 Store

如何使用 Pinia
Pinia 是 Vue 3 的状态管理库,它提供了开箱即用的开发生态系统,以便在 Vue 应用程序中管理状态。
安装
<code class="sh">npm install pinia</code>
创建 Store
立即学习“前端免费学习笔记(深入)”;
创建一个 store 模块:
<code class="js">import { defineStore } from 'pinia'
export const useCounterStore = defineStore('counter', {
state: () => ({
count: 0
}),
actions: {
increment() {
this.count++
},
decrement() {
this.count--
}
}
})</code>使用 Store
在 Vue 组件中使用 store:
<code class="vue"><script setup>
import { useCounterStore } from '../stores/counter'
const counterStore = useCounterStore()
</script>
<template>
<div>
<p>Count: {{ counterStore.count }}</p>
<button @click="counterStore.increment()">+</button>
<button @click="counterStore.decrement()">-</button>
</div>
</template></code>特性
Pinia 提供了以下特性:
以上就是vue3怎么使用pinia的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号