news 2026/6/23 11:32:21

Vue3使用pinia做全局状态管理的简单示例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue3使用pinia做全局状态管理的简单示例

一、插件版本:

"pinia": "^2.0.23",

"pinia-plugin-persist": "^1.0.0",

"vue": "^3.4.27"

二、store目录

src/store/index.ts

import type { App } from 'vue'; import { createPinia } from 'pinia'; import piniaPersist from 'pinia-plugin-persist'; const store = createPinia(); store.use(piniaPersist); export function setupStore(app: App<Element>) { app.use(store); } export { store };

src/store/tableStore.ts

import { Ref, ref } from 'vue'; import { defineStore } from 'pinia'; import { store } from '@/store'; export interface ITableStoreState { tableRef: Ref; list: Map<any, any>[]; } export const tableOrgStore = defineStore({ id: 'tableOrganization', state: (): ITableStoreState => ({ tableRef: ref(), list: [], }), persist: { enabled: true, }, getters: { getList(): ITableStoreState['list'] { return this.list; }, }, actions: { setList(info: ITableStoreState['list']) { this.list = info; }, }, }); // Need to be used outside the setup export function useTableStoreWidthOut() { return tableOrgStore(store); }

三、main.ts

。。。省略。。。 // 挂载状态管理 setupStore(app); 。。。省略。。。

四、在main.ts所在项目的任意组件中使用:

import { storeToRefs } from 'pinia'; import { useTableStoreWidthOut } from '@/store/modules/tableStore'; const tableStore = useTableStoreWidthOut(); const { tableRef, getList } = storeToRefs(tableStore); // 响应式数据使用storeToRefs包裹再解构获取 const { setList } = tableStore; // function方法直接解构获取

使用tableRef示例:

<template> <button @click="handleClick">click</button> <my-list ref="tableRef" /> </template> <script lang="ts" setup> import { storeToRefs } from 'pinia'; import { useTableStoreWidthOut } from '@/store/modules/tableStore'; import MyList from './MyList.vue'; const tableStore = useTableStoreWidthOut(); const { tableRef } = storeToRefs(tableStore); // 响应式数据使用storeToRefs包裹再解构获取 const handleClick = () => { console.log('===tableRef====', tableRef); }; </script>

五、参考资料

https://pinia.vuejs.org/core-concepts/

https://prazdevs.github.io/pinia-plugin-persistedstate/zh/guide/

https://cn.vuejs.org/api/application.html

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/23 19:48:22

用户体验调研问卷:LobeChat设计有效题目

用户体验调研问卷&#xff1a;LobeChat设计有效题目 在AI助手逐渐成为日常生产力工具的今天&#xff0c;用户不再满足于“能对话”的基础功能&#xff0c;而是期待一个懂我、顺手、可靠的智能伙伴。从ChatGPT掀起的交互革命开始&#xff0c;行业标准已被重新定义——流畅的打字…

作者头像 李华
网站建设 2026/6/23 21:01:53

BONT-L肽有效成分:棕榈酰六肽-19 Palmitoyl Hexapeptide-19

棕榈酰六肽-19通过放松肌肉和防止乙酰胆碱的释放来实现抗衰老效果&#xff0c;从而使皮肤看起来更年轻。棕榈酰六肽-19是BONT-L-Peptide中的有效成分&#xff0c;BONT-L-Peptide来自西班牙公司Infinitec。棕榈酰六肽-19能有效阻断从神经到面部肌肉的信号&#xff0c;防止收缩和…

作者头像 李华
网站建设 2026/6/23 16:47:56

1、Linux网络管理与资源指南

Linux网络管理与资源指南 1. 网络时代与Linux简介 在当今许多国家,互联网已成为家喻户晓的词汇,计算机网络正逐渐走向像电视机和微波炉一样普及。尽管网络的存在由来已久,但如今全球网络的迅速发展,使得即使是小型非营利组织和个人计算机用户也能轻松加入全球网络。 谈及…

作者头像 李华
网站建设 2026/6/23 21:00:50

9、Linux 下 PPP 协议的深入解析与配置指南

Linux 下 PPP 协议的深入解析与配置指南 1. PPP 协议概述 PPP(Point-to-Point Protocol)是一种用于通过串行连接发送数据报的协议,它解决了 SLIP 协议的一些不足。与 SLIP 不同,PPP 可以承载多种协议,不仅限于 IP 协议,还能在链路上进行错误检测。此外,PPP 允许通信双…

作者头像 李华
网站建设 2026/6/23 21:01:21

13、Linux网络重要特性全解析

Linux网络重要特性全解析 1. 网络应用基础 在成功设置好IP和解析器后,我们需要关注通过网络提供的服务。这里将介绍一些简单网络应用的配置,包括inetd服务器和rlogin家族的程序,还会简要提及远程过程调用(RPC)接口。不过,像网络文件系统(NFS)和网络信息系统(NIS)等…

作者头像 李华