当前位置: 首页 > news >正文

前端开发 Vue 状态优化

Vue 项目中的状态优化一般都会用Pinia替代Vuex,Pinia 是 Vue 生态系统中的一个轻量级状态管理库,作为 Vuex 的替代品,它提供了更简洁的 API 和更好的性能。

  • 模块化管理:使用 Pinia 时,建议将状态拆分为多个 store 模块,以避免单一状态树过于庞大和复杂。这不仅有助于维护,还能提升性能。

  • 懒加载 Store:通过 Pinia 的 defineStore 动态创建 store,当某个 store 仅在特定页面或组件中需要时,可以延迟加载它。这样可以减少应用的初始加载时间。

  • State 持久化:如果某些状态需要在页面刷新后保持,可以使用 Pinia 的插件功能将状态持久化到 localStorage 或 sessionStorage,避免不必要的网络请求或重新计算。

  • 避免不必要的深度响应:Pinia 允许你明确哪些状态需要响应式,哪些不需要。对于不需要响应式的复杂对象,可以使用 shallowRef 或 shallowReactive 来减少响应式开销。

1. 安装 Pinia 与配置

npm install pinia

设置 Pinia:

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'const app = createApp(App)// 创建 Pinia 实例
const pinia = createPinia()app.use(pinia)
app.mount('#app')

 2. 创建模块化 Store

创建两个 Store 模块:userStore 和 productStore。其中 userStore 将使用状态持久化,productStore 将演示懒加载和避免不必要的深度响应。

stores/userStore.js:

// stores/userStore.js
import { defineStore } from 'pinia'
import { ref, computed, watch } from 'vue'export const useUserStore = defineStore('user', () => {// 初始化状态,如果 localStorage 中有存储,优先使用存储的状态const name = ref(localStorage.getItem('user-name') || 'John Doe')const age = ref(parseInt(localStorage.getItem('user-age')) || 25)const doubleAge = computed(() => age.value * 2)// 监听状态变化,并将其保存到 LocalStoragewatch(() => name.value,(newValue) => {localStorage.setItem('user-name', newValue)})watch(() => age.value,(newValue) => {localStorage.setItem('user-age', newValue.toString())})return {name,age,doubleAge,}
})

stores/productStore.js:

// stores/productStore.js
import { defineStore } from 'pinia'
import { shallowRef } from 'vue'export const useProductStore = defineStore('product', () => {// 使用 shallowRef 来避免不必要的深度响应const products = shallowRef([])const addProduct = (product) => {products.value.push(product)}return {products,addProduct,}
})

3. 懒加载 Store

productStore 仅在需要时加载,例如在某个特定组件中。

components/ProductList.vue:

<template><div><h2>Product List</h2><ul><li v-for="product in products" :key="product.id">{{ product.name }}</li></ul><button @click="addNewProduct">Add Product</button></div>
</template><script setup>
import { useProductStore } from '@ stores/productStore'
import { onMounted } from 'vue'// 懒加载 productStore
const productStore = useProductStore()
const products = productStore.productsconst addNewProduct = () => {productStore.addProduct({ id: Date.now(), name: `Product ${products.length + 1}` })
}onMounted(() => {console.log('ProductList component mounted.')
})
</script>

4. 在其他组件中使用 userStore

components/UserProfile.vue:

<template><div><h2>User Profile</h2><p>Name: {{ name }}</p><p>Age: {{ age }}</p><p>Double Age: {{ doubleAge }}</p></div>
</template><script setup>
import { useUserStore } from '@/store/userStore'// 使用 userStore,这个 Store 状态会被持久化
const userStore = useUserStore()const { name, age, doubleAge } = userStore
</script>

5. 手动实现状态持久化

我们在 userStore 中通过 localStorage 手动实现了状态持久化。如果你需要更加通用的状态持久化插件,可以创建一个简单的 Pinia 插件。

plugins/persistedState.js:

// plugins/persistedState.js
export function createPersistedStatePlugin(options = {}) {return ({ store }) => {const { key = store.$id } = options// 从 LocalStorage 初始化状态const fromStorage = localStorage.getItem(key)if (fromStorage) {store.$patch(JSON.parse(fromStorage))}// 订阅状态变化,并将其保存到 LocalStoragestore.$subscribe((mutation, state) => {localStorage.setItem(key, JSON.stringify(state))})}
}

注册插件

import { createPinia } from 'pinia'
import { createPersistedStatePlugin } from './plugins/persistedState'const pinia = createPinia()
pinia.use(createPersistedStatePlugin())const app = createApp(App)
app.use(pinia)
app.mount('#app')
http://www.dtcms.com/a/298772.html

相关文章:

  • 多场景通用车辆计数算法助力暑期交通管理
  • Java从入门到精通!第十四天,重点!(反射)
  • 20250725-day22
  • Ivanti Endpoint Manager Mobile 远程命令执行漏洞复现(CVE-2025-4427)
  • 壁纸管理 API 文档
  • 测试实时性内核参数配置
  • 如何阅读字节码文件
  • Arrays 工具类详解
  • 在线事务型的业务、实时分析类业务、离线处理类型的业务
  • C语言————原码 补码 反码 (超绝详细解释)
  • 【循环语句,求100内能被6整除的和】
  • 群晖 File Station:集中浏览与管理 NAS 文件的工具
  • 60个Java与Spring核心知识点详解
  • [Java恶补day46] 整理模板·考点九【二叉树最近公共祖先】
  • 跨境电商流量密码:自养号测评采购技术,低成本撬动高转化
  • agent含义、起源、定义、分类、应用场景以及未来趋势
  • 机器学习入门
  • 从 “能打” 到 “顶尖”:DeepSeek-V3 后训练拆解,微调 + 强化学习如何让大模型脱胎换骨?
  • html+js列表分页功能封装
  • Kubernetes服务发布进阶
  • GPT - 5被曝将在8月初发布!并同步推出mini、nano版
  • 如何评估一个RWA项目的可信度?关键指标解析
  • dart使用
  • 在DolphinScheduler执行Python问题小记
  • 关于linux运维 出现高频的模块认知
  • 基于多种主题分析、关键词提取算法的设计与实现【TF-IDF算法、LDA、NMF分解、BERT主题模型】
  • Nginx 限流
  • Model Control Protocol 三层架构设计,三种传输方式,完成MCP项目构建实现工具调试,多维度评价指标检测多工具多资源调用的鲁棒性和稳健性
  • MyBatisPlus(一)简介与基本CRUD
  • Logcat日志分析