Pinna的使用以及pinna的持续化存储(两种方法)

news/2024/10/17 16:22:50/

一、简介

pinia是 vue3 新的状态管理工具,简单来说相当于之前 vuex,它去掉了 Mutations 但是也是支持 vue2 的,需要的朋友可以参考下

二、使用方法

1.安装

npm install pinia -S

2..引入

import { createPinia,PiniaPluginContext } from 'pinia'

import { createApp,toRaw } from 'vue'

import App from './App.vue'

const store = createPinia()

const app  = createApp(App)

app.use(store)

app.mount('#app')

3.创建store文件夹

在src目录下面,创建store文件

注意: pinia 不需要创建 modules 文件来区分模块化,这是它和 vuex 的区别。

 store/index.ts

import {defineStore } from 'pinia'
import {Names} from './store-name'
type User = {name:string,age:number
}const result:User = {name:'xiaochen',age:888,
}const asyncResult:User ={name:'异步名字',age:999,
}const Login = ():Promise<User> =>{return new Promise((resolve) =>{setTimeout(()=>{resolve(asyncResult)},2000)})
}
//第一个参数 命名空间唯一值
export const useTestStore = defineStore(Names.TEST,{state:()=>{return{current:122,name:'xiaochen2',user:<User>{},asyncUser:<User>{},}},//相当于computed 修饰一些值 有缓存的getters:{newName ():string{return `$-${this.name}-${this.getUserAge}`},getUserAge():number{return this.user.age || 1}},//methods 可以做同步 异步也支持 提交stateactions:{// setCurrent(num:number){//     this.current = num// },//同步setUser(){this.user = result},//异步async getUser(){const resultList = await Login()this.asyncUser = resultListthis.setName('超级帅的飞机')//相互调用方法this.setAge(6666)//相互调用方法},setName (name:string){this.name = name},setAge (age:number){this.user.age = age}}}) 

命名空间的唯一值

export const enum Names {TEST = 'TEST',BASE = 'BASE'
}

 

4.使用Pinia

import { useTestStore} from "../store";

const sinaTest = useTestStore();

5.修改数据(五种方法)

方法1:直接改变

<div class="container">

    Pinia:{{ sinaTest.current }} -- {{ sinaTest.name }}

    <button @click="change">改变</button>

</div>

const change = () => {

  sinaTest.current++;

};

方法2:利用patch直接改变

const change = () => {

  sinaTest.$patch({

    current: 888,

    name: "黑丝",

  });

};

方法3:利用$patch传递参数

const change = () => {

  //可以写工厂函数

  sinaTest.$patch((state) => {

    state.current = 999;

    state.name = "迪丽热巴";

  });

};

方法4: 利用$state修改(有弊端不推荐)

const change = () => {

  //可以写工厂函数

  sinaTest.$state = {

    current: 2000,

    name: "134",

  };

};

方法5: 通过actions去改变

const change = () => {

  sinaTest.setCurrent(852);

};

这里只是简单了一些修改的方法 ,具体使用还是得结合业务场景去使用,具体了解可以看看Pinia官方文档

三、持久化存储

pinia 和 vuex 一样,数据是短时的,只要一刷新页面,数据就会恢复成初始状态,为了避免这个问题,可以对其采用持久化保存方法。

持久化保存的原理是在 pinia 中数据更新时,同步保存到 localStorage 或 sessionStorage 中,刷新后从本地存储中读取数据,

下面介绍两种实现的方法

一:手写

1.自己手写一个实现持久化(在main.ts写)

首先我们先了解一下pinpa能获取到的参数

通过上面的打印我们了解到对应的数据存在哪里,我们即可以手写

//先手写我们存储数据的localStorage
const getStorage = (key:string) =>{return localStorage.getItem(key) ? JSON.parse(localStorage.getItem(key) as string):{}}//存储数据const setStorage = (key:string,value:any) => {localStorage.setItem(key,JSON.stringify(value)) //value可能是对象或者数组 所以JSON.stringify需要这个转换成字符串 ['1','2','3'] => '['1','2','3']'}

 2.手写我们插件函数(柯里化优化):柯里化就是将一个多参数的函数  转换成单参数的函数

解析:options?.key ??__piniaKey__          当key不传的时候默认是__piniaKey__ ,传的时候就是传的数据。下面使用的时候有介绍

空值合并运算符( ?? )是一个逻辑操作符,如果左侧的表达式为 null或者 undefined 时,返回其右侧表达式,否则返回左侧表达式。

//函数柯里化  柯里化就是将一个多参数的函数  转换成单参数的函数
const piniaPlugin = (options:Options) =>{return (context:PiniaPluginContext)=>{ //拿取到pinpa的参数console.log('context',context);const {store} = context //解构我们需要的storeconst data = getStorage(`${options?.key ??__piniaKey__}-${store.$id}`) //获取到keyconsole.log('data',data);//可以通过$subscribe监测到store数据变化store.$subscribe(()=>{// 每当它发生变化时,将整个状态持久化到本地存储//${options?.key ??__piniaKey__}-${store.$id} 唯一值keysetStorage(`${options?.key ??__piniaKey__}-${store.$id}`,toRaw(store.$state)) //store.$state proxy对象  toRaw把它转为原始对象})return{...data}}
}

3.使用我们的插件

store.use(piniaPlugin({key:'pinia' //传值key过去 存储用这个名字
}))

4.效果如下

 二、使用插件

1.安装插件

npm install pinia-plugin-persist

2.引入插件

import piniaPersist from 'pinia-plugin-persist'

pinia.use(piniaPersist)

3.使用插件

设置key,指定保存内容

export const useBaseStore = defineStore(Names.BASE, {state: () => {return {baseUrl: "http://localhost:9090",BaseCurrent: 0,};},persist: {enabled: true, // true 表示开启持久化保存strategies: [{key: 'base',storage: localStorage,paths: ['BaseCurrent','baseUrl']},],},//相当于computed 修饰一些值 有缓存的getters: {},//methods 可以做同步 异步也支持 提交stateactions: {},
}); //第一个参数 命名空间唯一值

②你甚至可以对不同数据设置不同的本地存储方式。

export const useBaseStore = defineStore(Names.BASE, {state: () => {return {baseUrl: "http://localhost:9090",BaseCurrent: 0,testType:'test',};},persist: {enabled: true, // true 表示开启持久化保存strategies: [{key: 'base',storage: localStorage,paths: ['BaseCurrent','baseUrl']},{key: 'base2',storage: sessionStorage,paths: ['testType']},],},//相当于computed 修饰一些值 有缓存的getters: {},//methods 可以做同步 异步也支持 提交stateactions: {},
}); //第一个参数 命名空间唯一值

         

③保存到 cookie

保存到 cookie 中当然也是可以的,不过需要手动添加 cookie 的保存方式,同样,此处也是推荐使用插件 js-cookie 来完成。一般用于存储token

npm install js-cookie

 

import Cookies from 'js-cookie'
//存储在cookie
const cookiesStorage: Storage = {setItem(key, state:any) {console.log('state',state);return Cookies.set(key, state, { expires: 3 }); // 设置有效期 3 天,不设置默认同 sessionStorage 有效期一致},getItem(key) {return JSON.stringify({accessToken: Cookies.get(key),});},length: 0,clear: function (): void {throw new Error("Function not implemented.");},key: function (index: number): string | null {throw new Error("Function not implemented.");},removeItem: function (key: string): void {throw new Error("Function not implemented.");}
}export const useBaseStore = defineStore(Names.BASE, {state: () => {return {baseUrl: "http://localhost:9090",BaseCurrent: 0,testType:'test',token:'12314',};},persist: {enabled: true, // true 表示开启持久化保存strategies: [{key: 'base',storage: localStorage,paths: ['BaseCurrent','baseUrl']},{key: 'base2',storage: sessionStorage,paths: ['testType']},{key: 'tokenSave',storage: cookiesStorage,paths: ['token']},],},//相当于computed 修饰一些值 有缓存的getters: {},//methods 可以做同步 异步也支持 提交stateactions: {},
}); //第一个参数 命名空间唯一值

 


http://www.ppmy.cn/news/86659.html

相关文章

1行代码合并多个PPT文件,Python自动化办公

大家好&#xff0c;这里是程序员晚枫&#xff0c;小破站也叫这个名字。 今天继续给大家更新自动化办公的功能&#xff1a;1行代码&#xff0c;合并多个PPT文件。 最近刚换了新工作&#xff0c;功能更新比较慢。大家有需要直接留言&#xff0c;每个留言我都会回复~ 1、上代码 …

公司只有1个测试,领导却让我测试10个项目,这不是为难我....

读者提问&#xff1a;公司只有 1个测试&#xff0c;领导让我同时测试 10个项目&#xff0c;我该怎么办&#xff1f;回答&#xff1a;如果我是那个测试&#xff0c;我会做这三件事 1、向上申请资源2、任务分配到人3、执行测试任务 一、向上申请资源 1个测试同时对接 10个项目&a…

Holocube-第一集

准备工作&#xff1a; 装备&#xff1a; ESP 8266 D1 MINI1.3寸 TFT 屏幕分光棱镜25.4mm面包板、杜邦线、数据线等配件 软件&#xff1a; Arduino(本人使用1.8.16)---驱动ch341 初始arduino简单程序&#xff0c;对esp8266上传代码&#xff0c;能正常运行。正式开始 坑1&…

怎么通过ecs云服务器来给小程序发送消息

如果您想通过 ECS 云服务器向小程序发送消息&#xff0c;可以使用 WebSocket 技术。具体步骤如下&#xff1a; 1. 在 ECS 云服务器上搭建 WebSocket 服务器。您可以使用 Node.js、Java、Python 等编程语言来实现 WebSocket 服务器&#xff0c;具体实现方式可参考相关技术文档或…

Linux系统初始化命令的备忘单,Linux运维工程师收藏!

在管理和维护Linux系统时&#xff0c;有一些常用的命令可以帮助您进行系统初始化和配置。这些命令涵盖了各种任务&#xff0c;包括系统设置、用户管理、软件安装和网络配置等。 本文将为您提供一个Linux系统初始化命令的备忘单&#xff0c;以便在需要时方便查阅和使用。 系统设…

C语言指针

C语言指针 前言1. 指针是什么&#xff1f;1.2 如何编址呢&#xff1f; 2. 指针和指针类型2.1 指针-整数2.2 指针类型的意义 3. 野指针3.1 野指针成因3.1.1 指针未初始化3.1.2 指针越界访问1.1.3 指针指向的空间释放 3.2 如何避免野指针 4. 指针运算4.1 指针 /- 指针4.2 指针 -…

【基于ROS Melodic环境安装rosserial arduino】

【基于ROS Melodic环境安装rosserial arduino】 1. 简介2. 安装2.1 Ubuntu下的Arduino IDE安装2.2 Ubuntu下rosserial arduino软件安装2.3 安装ros_lib到Arduino IDE开发环境 3. 将ros_lib配置到 Arduino 环境库中4. 使用helloword5. 实验验证6.总结 1. 简介 这个教程展示如何…

总结879

学习目标&#xff1a; 月目标&#xff1a;5月&#xff08;1800基础部分&#xff0c;背诵15篇短文&#xff0c;熟词僻义300词基础词&#xff09; 周目标&#xff1a;1800高等数学部分并完成错题记录&#xff0c;英语背3篇文章并回诵 每日必复习&#xff08;5分钟&#xff09; …