vue3 pinia状态管理

ops/2025/2/19 2:55:37/

文章目录

    • 安装 Pinia
    • 创建 Pinia 实例
    • 定义 Store
    • 在组件中使用 Store
    • pinia 增删改查
    • 在组件中使用
    • 响应式和插件
      • 1. 安装插件
      • 2. 配置 Pinia 插件
      • 3. 在 Store 中启用持久化
      • 4. 自定义持久化配置

安装 Pinia

首先,你需要安装 Pinia。可以通过 npm 或 yarn 来进行安装:

npm install pinia
yarn add pinia

创建 Pinia 实例

在你的 Vue 应用程序中创建 Pinia 实例,并将其传递给 Vue 应用。通常在 main.js 或 main.ts 文件中进行:

// 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');

定义 Store

Pinia 的核心概念是“store”。你可以创建一个 store 来管理相关状态。下面是一个简单的示例:

// 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++;},decrement() {this.count--;},},
});

在组件中使用 Store

一旦定义了 store,你可以在任何组件中使用它。下面是如何在组件中使用 useCounterStore 的示例:

<template><div><h1>{{ count }}</h1><h2>{{ doubleCount }}</h2><button @click="increment">Increment</button><button @click="decrement">Decrement</button></div>
</template><script>
import { useCounterStore } from '@/stores/counter';export default {setup() {const counterStore = useCounterStore();return {count: counterStore.count,doubleCount: counterStore.doubleCount,increment: counterStore.increment,decrement: counterStore.decrement,};},
};
</script>

pinia__78">pinia 增删改查

// stores/taskStore.js
import { defineStore } from 'pinia';export const useTaskStore = defineStore('task', {state: () => ({tasks: [],}),actions: {addTask(task) {this.tasks.push(task);},removeTask(taskId) {this.tasks = this.tasks.filter(task => task.id !== taskId);},updateTask(updatedTask) {const index = this.tasks.findIndex(task => task.id === updatedTask.id);if (index !== -1) {this.tasks[index] = updatedTask;}},getTask(taskId) {return this.tasks.find(task => task.id === taskId);},},
});

在组件中使用

<template><div><h1>任务管理</h1><input v-model="newTask" placeholder="新任务名称" /><button @click="addNewTask">添加任务</button><ul><li v-for="task in taskStore.tasks" :key="task.id">{{ task.name }}<button @click="removeTask(task.id)">删除</button><button @click="updateTask(task)">更新</button></li></ul></div>
</template><script>
import { ref } from 'vue';
import { useTaskStore } from '../stores/taskStore';export default {setup() {const taskStore = useTaskStore();const newTask = ref('');const addNewTask = () => {if (newTask.value.trim()) {taskStore.addTask({ id: Date.now(), name: newTask.value });newTask.value = ''; // 清空输入框}};const removeTask = (taskId) => {taskStore.removeTask(taskId);};const updateTask = (task) => {const updatedName = prompt('更新任务名称:', task.name);if (updatedName !== null) {taskStore.updateTask({ ...task, name: updatedName });}};return { taskStore, newTask, addNewTask, removeTask, updateTask };},
};
</script>

响应式和插件

Pinia 的状态是响应式的,因此当你更新状态时,任何依赖于该状态的组件都会自动重新渲染。

同时,你也可以为 Pinia 添加插件来实现诸如持久化状态、日志等功能。例如,如果你想要持久化状态,可以使用 pinia-plugin-persistedstate 插件。

1. 安装插件

首先,你需要在项目中安装 pinia-plugin-persistedstate。可以使用 npm 或 yarn 进行安装

# 使用 npm 安装
npm install pinia-plugin-persistedstate# 使用 yarn 安装
yarn add pinia-plugin-persistedstate

2. 配置 Pinia 插件

在创建 Pinia 实例时,需要将 pinia-plugin-persistedstate 作为插件添加进去。以下是一个示例代码:

// main.js 或 main.ts
import { createApp } from 'vue';
import { createPinia } from 'pinia';
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
import App from './App.vue';// 创建 Pinia 实例
const pinia = createPinia();
// 使用持久化插件
pinia.use(piniaPluginPersistedstate);const app = createApp(App);
// 将 Pinia 实例挂载到应用中
app.use(pinia);
app.mount('#app');

3. 在 Store 中启用持久化

在定义 Pinia store 时,可以通过 persist 选项来启用持久化。以下是一个简单的 store 示例:

// store/counter.js 或 store/counter.ts
import { defineStore } from 'pinia';export const useCounterStore = defineStore('counter', {state: () => ({count: 0}),actions: {increment() {this.count++;}},// 启用持久化persist: true
});

4. 自定义持久化配置

你还可以对持久化进行更详细的配置,例如指定存储方式(localStorage 或 sessionStorage)、存储的键名等。以下是一个自定义配置的示例:

// store/counter.js 或 store/counter.ts
import { defineStore } from 'pinia';export const useCounterStore = defineStore('counter', {state: () => ({count: 0}),actions: {increment() {this.count++;}},persist: {// 存储方式,默认为 localStoragestorage: localStorage, // 存储的键名,默认为 store 的 idkey: 'my-counter-store', // 只持久化指定的状态paths: ['count'] }
});

在上述示例中,通过 storage 选项指定了存储方式为 localStorage,通过 key 选项指定了存储的键名,通过 paths 选项指定了只持久化 count 状态。


http://www.ppmy.cn/ops/158799.html

相关文章

cmake项目,多模块的一些自我摸索

我的项目是&#xff1a;有一个根cmakelist&#xff0c;下面有若干个子项目&#xff0c;在这个基础上&#xff0c;需要在根下的common目录下存放公用的代码文件 如果你的项目结构是一个根 CMakeLists.txt 管理若干个子项目&#xff0c;并且需要在 common 目录下存放公共代码文件…

CTF-web:java-h2 堆叠注入rce -- N1ctf Junior EasyDB

代码存在sql注入 // 处理登录表单的POST请求PostMapping({"/login"})public String handleLogin(RequestParam String username, RequestParam String password, HttpSession session, Model model) throws SQLException {// 验证用户凭据if (this.userService.valid…

Grafana-使用Button修改MySQL数据库

背景 众所周知&#xff0c;Grafana是一个用来展示数据的平台&#xff0c;但是有时候还是会有需求说能不能有一个按钮&#xff0c;点击的时候再对数据库进行修改&#xff0c;从而达到更新数据的效果 经过多方查证&#xff0c;终于实现了一个简单的&#xff0c;点击button执行sq…

ArcGIS注册开发账号及API KEY

注册与激活 Sign up | ArcGIS Location Platform 填写信息&#xff0c;然后邮箱收到激活邮件&#xff0c;激活&#xff0c;再补充信息。 参考 Tutorial: Create an API key | Documentation | Esri Developer 产生API KEY Tutorial: Create an API key | Documentation |…

JavaScript作用域与闭包

一 作用域 在JavaScript中&#xff0c;作用域&#xff08;Scope&#xff09;指的是变量和函数的可访问性范围。在JavaScript中&#xff0c;作用域有全局作用域和局部作用域之分。 全局作用域&#xff08;Global Scope&#xff09;&#xff1a;全局作用域指的是在代码中任何位置…

ASP.NET Core SixLabors.ImageSharp v1.0 的图像实用程序类 web示例

这个小型实用程序库需要将 NuGet SixLabors.ImageSharp包&#xff08;版本 1.0.4&#xff09;添加到.NET Core 3.1/ .NET 6 / .NET 8项目中。它与Windows、Linux和 MacOS兼容。 这已针对 ImageSharp v3.0.1 进行了重新设计。 它可以根据百万像素数或长度乘以宽度来调整图像大…

C++17中的clamp函数

一、std::clamp() 其实在前面简单介绍过这个函数&#xff0c;但当时只是一个集中的说明&#xff0c;为了更好的理解std::clamp的应用&#xff0c;本篇再详细进行阐述一次。std::clamp在C17中其定义的方式为&#xff1a; template< class T > constexpr const T& cl…

二叉树(C语言版)

文章目录 二叉树完全二叉树和满二叉树二叉搜索树基本操作实现代码运行结果 分析红黑树2-3-4树(理论模型)红黑树(实际实现) 二叉树 树是一种层次结构&#xff0c;它在现实生活中是广泛存在的&#xff0c;比如&#xff1a;族谱(family tree)&#xff0c;组织机构&#xff0c;目录…