码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Pinia 及其数据持久化 Vue新一代状态管理插件


    黑马前端Vue新一代状态管理插件Pinia快速入门视频教程
    Pinia主页

    超级简单,不需要耐心

    pinia :新一代的VueX

    1. 安装

    npm install pinia
    
    • 1

    2. 在main.js中引入

    import { createPinia } from 'pinia'
    app.use(createPinia())
    
    • 1
    • 2

    3. 新建stores目录,新建.js文件

    pinia的使用和在setup中写的代码基本一致,记得`ruturn`数据就行
    
    • 1
    // path:@/stores/counter.js
    import {computed, ref} from "vue";
    import {defineStore} from 'pinia'
    // 第一个参数是 store 的唯一标识id
    export const useCounterStore = defineStore('counter',()=>{
        // VueX:state,数据
        const count = ref(0)
        // VueX:getters,计算属性
        const doubleCount = computed(() => count.value * 2)
        // VueX:actions + mutations,方法
        function increment(){
            count.value++
        }
        return { count, doubleCount, increment }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4. 使用

    在vue中导入pinia后需要**赋值操作**,然后使用`.`来访问pinai中的数据和方法
    
    • 1
    App.vue
    <template>
        计数器:{{countStore.count}}<br>
        加倍计数器:{{countStore.doubleCount}}<br>
        <button @click="countStore.increment">点我加1button>
    template>
    
    <script setup>
    // 引入stroe文件
    import {useCounterStore} from "@/stores/counter"
    const countStore = useCounterStore()
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5. 另一种使用方式,对store解构,不使用countStore.去访问store中的数据和方法

    <template>
        计数器:{{count}}<br>
        加倍计数器:{{doubleCount}}<br>
        <button @click="increment">点我加1button>
    template>
    
    <script setup>
    import {useCounterStore} from "@/stores/counter"
    import {storeToRefs} from "pinia";
    // 得到store的实例对象
    const countStore = useCounterStore()
    // 对state的解构需要使用storeToRefs方法
    const { count, doubleCount } = storeToRefs(countStore)
    // 对action的结构,直接干
    const {increment} = countStore
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    pinia-plugin-persistedstate : 数据持久化

    视频
    主页

    1. 安装

    npm : npm i pinia-plugin-persistedstate
    
    • 1

    2. main.js中pinia使用插件

    import { createApp } from 'vue'
    import App from './App.vue'
    
    import { createPinia} from 'pinia'
    import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
    
    const pinia = createPinia() // 创建pinia实例
    pinia.use(piniaPluginPersistedstate) // pinia使用持久化插件
    createApp(App).use(pinia).mount('#app')
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3. 最简单粗暴的用法,将state数据保存在localstorage(关闭浏览器数据清空)

    在这里插入图片描述
    查看保存在localstorage中的结果
    在这里插入图片描述

    4. 保存到session storage中(session storage关闭页面丢失数据)

    只需要给persist加个对象,paths中是要保存的数据的名字
    
    • 1
        persist: {
            storage: sessionStorage,
            paths:['count']
        }
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    什么是持久化?

    1. 非持久化

    在这里插入图片描述

    2. 持久化

    在这里插入图片描述

  • 相关阅读:
    用 GPL 开源后想转闭源,法院判决 GPL 协议终身有效;与 Log4j 漏洞斗争是场持久战;Firefox 96 发布 | 开源日报
    几种典型的深度学习算法:(CNN、RNN、GANS、RL)
    JVM的内存区域划分
    Redis 列表( List )
    软件测试功能测试全套常见面试题【开放性思维题】面试总结4-3
    MFC Windows 程序设计[153]之多样的文档视图
    30、三维表面重建-Convolutional Occupancy Network
    【毕业设计】深度学习交通车辆流量分析 - 目标检测与跟踪 - python opencv
    大数据在电力行业的应用案例100讲(十二)-全链路设计
    CentOS7二进制方式安装Docker
  • 原文地址:https://blog.csdn.net/YUELEI118/article/details/134434869
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号