前端学习笔记-vue状态管理优化

15 扁平化你的 Store,合理使用持久化

一句话理解

Store(Pinia / Vuex)里的数据结构要像数据库表一样扁平 (用 id 做关联,别深层嵌套);持久化(存 localStorage)要有选择,别什么都存。

为什么

  • 深层嵌套的 Store → 更新困难、响应式开销大(呼应第 07 条)。比如要改"文章 5 的评论 3 的点赞数",深层路径又慢又容易写错。
  • 扁平化(normalized) → 用 { [id]: item } 的 map 结构,查找 O(1),更新精准。
  • 持久化全存 → localStorage 有容量限制(5MB 左右),存太多会卡、还会存进敏感数据。

代码示例

反例:嵌套 + 全量持久化:

js 复制代码
// 嵌套结构:改一个评论的点赞数要穿透 3 层
const badStore = {
  posts: [
    {
      id: 1,
      title: '...',
      comments: [
        { id: 10, likes: 5, author: { id: 7, name: '张三' } }
      ]
    }
  ]
}

正例:扁平化(像数据库表):

js 复制代码
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useForumStore = defineStore('forum', () => {
  // 三张"表",用 id 关联,互不嵌套
  const posts = ref({})           // { [postId]: post }
  const comments = ref({})        // { [commentId]: comment }
  const users = ref({})           // { [userId]: user }

  // O(1) 查找某篇文章
  const getPost = (id) => posts.value[id]

  // 精准更新某条评论的点赞数,不影响其它数据
  const likeComment = (commentId) => {
    comments.value[commentId].likes++
  }

  return { posts, comments, users, getPost, likeComment }
})

有选择地持久化(只存该存的):

js 复制代码
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  const token = ref('')        // 持久化:登录态
  const profile = ref({})      // 持久化:用户资料

  const tempCaptcha = ref('')  // 不持久化:临时验证码
  const formDraft = ref({})    // 不持久化:表单草稿

  // 手动持久化关键数据(或用 pinia-plugin-persistedstate 按需配置)
  function persist() {
    localStorage.setItem('token', token.value)
  }

  return { token, profile, tempCaptcha, formDraft, persist }
})
</script>

新人提示

  • 扁平化的核心思想:数据按"实体"分表,用 id 互相引用,和后端数据库设计同源。
  • 持久化的判断:"刷新页面后还需要吗?" 需要(如登录态)→ 存;不需要(如临时状态)→ 不存。
  • 别持久化敏感数据(密码、密钥)到 localStorage,它对任何 JS 都开放。
相关推荐
从零开始的代码生活_1 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
可乐奶茶sky2 小时前
AI Agent 学习
人工智能·学习
摇滚侠2 小时前
Java 全栈开发实战教程 课程笔记 29-33
笔记
GIS阵地2 小时前
QgsSingleBandPseudoColorRenderer 完整详解(QGIS 3.40.13 C++)
开发语言·前端·c++·qt·qgis
茯苓gao2 小时前
嵌入式开发笔记:EtherCAT协议从硬件到软件完整配置指南——从零搭建一套EtherCAT通信系统
笔记·嵌入式硬件·学习
刘较瘦_3 小时前
AI 开发中的 Git Submodule 父子仓库模式:前后端分仓管理与协作实践
前端·github
牧艺3 小时前
cos-design WeatherBackground:用 Canvas 做一个「会变天」的背景引擎
前端·canvas·视觉设计
OpenTiny社区3 小时前
深度解析 LSP 如何为 AI 装上“眼睛”
前端·ai编程
whyTeaFo3 小时前
GAMES101: Lecture 9: Shading 3(Texture Mapping cont.) ppt笔记
笔记