Pinia简单使用以及数据持久化详解

作者:壹个切图仔 时间:2024-05-28 15:52:59 

基本介绍

Pinia 是 Vue.js 的轻量级状态管理库

官方网站:pinia.vuejs.org/

  • pinia和vuex4一样,也是vue官方的状态管理工具(作者是 Vue 核心团队成员)

  • pinia相比vuex4,对于vue3的兼容性更好

  • pinia相比vuex4,具备完善的类型推荐

  • pinia同样支持vue开发者工具,最新的开发者工具对vuex4支持不好

pinia核心概念

  • state: 状态

  • actions: 修改状态(包括同步和异步,pinia中没有mutations)

  • getters: 计算属性

基本使用与state

目标:掌握pinia的使用步骤

(1)安装

yarn add pinia
# or
npm i pinia

(2)在main.js中挂载pinia

import { createApp } from 'vue'
import App from './App.vue'

import { createPinia } from 'pinia'
const pinia = createPinia()

createApp(App).use(pinia).mount('#app')

(3)新建文件store/counter.js

import { defineStore } from 'pinia'
// 创建store,命名规则: useXxxxStore
// 参数1:store的唯一表示
// 参数2:对象,可以提供state actions getters
const useCounterStore = defineStore('counter', {
 state: () => {
   return {
     count: 0,
   }
 },
 getters: {

},
 actions: {

},
})

export default useCounterStore

(4) 在组件中使用

<script setup>
import useCounterStore from './store/counter'

const counter = useCounterStore()
</script>

<template>
 <h1>根组件---{{ counter.count }}</h1>
</template>

<style></style>

actions的使用

目标:掌握pinia中actions的使用

在pinia中没有mutations,只有actions,不管是同步还是异步的代码,都可以在actions中完成。

(1)在actions中提供方法并且修改数据

import { defineStore } from 'pinia'
// 1. 创建store
// 参数1:store的唯一表示
// 参数2:对象,可以提供state actions getters
const useCounterStore = defineStore('counter', {
 state: () => {
   return {
     count: 0,
   }
 },
 actions: {
   increment() {
     this.count++
   },
   incrementAsync() {
     setTimeout(() => {
       this.count++
     }, 1000)
   },
 },
})

export default useCounterStore

(2)在组件中使用

<script setup>
import useCounterStore from './store/counter'

const counter = useCounterStore()
</script>

<template>
 <h1>根组件---{{ counter.count }}</h1>
 <button @click="counter.increment">加1</button>
 <button @click="counter.incrementAsync">异步加1</button>
</template>

getters的使用

pinia中的getters和vuex中的基本是一样的,也带有缓存的功能

(1)在getters中提供计算属性

import { defineStore } from 'pinia'
// 1. 创建store
// 参数1:store的唯一表示
// 参数2:对象,可以提供state actions getters
const useCounterStore = defineStore('counter', {
 state: () => {
   return {
     count: 0,
   }
 },
 getters: {
   double() {
     return this.count * 2
   },
 },
 actions: {
   increment() {
     this.count++
   },
   incrementAsync() {
     setTimeout(() => {
       this.count++
     }, 1000)
   },
 },
})

export default useCounterStore

(2)在组件中使用

<h1>根组件---{{ counter.count }}</h1>
 <h3>{{ counter.double }}</h3>

storeToRefs的使用

目标:掌握storeToRefs的使用

如果直接从pinia中解构数据,会丢失响应式, 使用storeToRefs可以保证解构出来的数据也是响应式的

<script setup>
import { storeToRefs } from 'pinia'
import useCounterStore from './store/counter'

const counter = useCounterStore()
// 如果直接从pinia中解构数据,会丢失响应式
const { count, double } = counter

// 使用storeToRefs可以保证解构出来的数据也是响应式的
const { count, double } = storeToRefs(counter)
</script>

pinia模块化

在复杂项目中,不可能把多个模块的数据都定义到一个store中,一般来说会一个模块对应一个store,最后通过一个根store进行整合

(1)新建store/user.js文件

import { defineStore } from 'pinia'

const useUserStore = defineStore('user', {
 state: () => {
   return {
     name: 'zs',
     age: 100,
   }
 },
})

export default useUserStore

(2)新建store/index.js

import useUserStore from './user'
import useCounterStore from './counter'

// 统一导出useStore方法
export default function useStore() {
 return {
   user: useUserStore(),
   counter: useCounterStore(),
 }
}

(3)在组件中使用

<script setup>
import { storeToRefs } from 'pinia'
import useStore from './store'
const { counter } = useStore()

// 使用storeToRefs可以保证解构出来的数据也是响应式的
const { count, double } = storeToRefs(counter)
</script>

pinia数据持久化

目标: 通过 Pinia 插件快速实现持久化存储。

插件文档:点击查看

用法

安装

yarn add pinia-plugin-persistedstate
or
npm i  pinia-plugin-persistedstate

使用插件 在main.ts中注册

import { createApp } from "vue";
import App from "./App.vue";
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);
createApp(App).use(pinia);

模块开启持久化

const useHomeStore = defineStore("home",{
 // 开启数据持久化
 persist: true
 // ...省略
});

常见疑问

  • 模块做了持久化后,以后数据会不会变,怎么办?

    • 先读取本地的数据,如果新的请求获取到新数据,会自动把新数据覆盖掉旧的数据。

    • 无需额外处理,插件会自己更新到最新数据。

进阶用法

需求:不想所有数据都持久化处理,能不能按需持久化所需数据,怎么办?

  • 可以用配置式写法,按需缓存某些模块的数据。

import { defineStore } from 'pinia'

export const useStore = defineStore('main', s{
 state: () => {
   return {
     someState: 'hello pinia',
     nested: {
       data: 'nested pinia',
     },
   }
 },
 // 所有数据持久化
 // persist: true,
 // 持久化存储插件其他配置
 persist: {
   // 修改存储中使用的键名称,默认为当前 Store的 id
   key: 'storekey',
   // 修改为 sessionStorage,默认为 localStorage
   storage: window.sessionStorage,
   // 部分持久化状态的点符号路径数组,[]意味着没有状态被持久化(默认为undefined,持久化整个状态)
   paths: ['nested.data'],
 },
})

总结:相比于vuex,pinia对于typescript的支持性更好,友好的devTools支持,pinia只有1kb,简化了很多方法的写法。

来源:https://juejin.cn/post/7101657189428756516

标签:pinia,持久化,数据
0
投稿

猜你喜欢

  • Python人脸识别初探

    2023-01-24 09:39:58
  • python 读取修改pcap包的例子

    2023-11-29 23:56:48
  • access改mdb为asp所带来的灾难 附mdb防下载方法

    2011-03-03 11:07:00
  • 设计英文网站要注意的问题

    2011-04-28 11:22:00
  • Python数据分析之matplotlib绘图详解

    2022-11-21 16:34:30
  • MySQL索引结构详细解析

    2024-01-13 19:20:06
  • Python中使用scapy模拟数据包实现arp攻击、dns放大攻击例子

    2021-12-04 12:15:05
  • Python搭建HTTP服务过程图解

    2023-08-08 22:11:54
  • 微信小程序创建自定义全局函数以及其调用方法详解

    2023-08-24 20:43:22
  • Python使用pyecharts控件绘制图表

    2023-11-08 17:59:54
  • 一文带你了解Python枚举类enum的使用

    2022-05-27 07:46:51
  • Python如何使用27行代码绘制星星图

    2024-01-02 10:41:44
  • python字符串不可变数据类型

    2021-04-14 23:07:09
  • python 读取竖线分隔符的文本方法

    2023-03-12 14:45:14
  • numpy中loadtxt 的用法详解

    2022-03-21 09:46:39
  • Warning: require(): open_basedir restriction in effect,目录配置open_basedir报错问题分析

    2023-06-02 23:28:18
  • vue项目页面的打印和下载PDF加loading效果的实现(加水印)

    2024-04-30 10:29:39
  • Python实现SVN的目录周期性备份实例

    2021-07-19 16:13:22
  • 详解Python中contextlib上下文管理模块的用法

    2022-03-10 22:32:51
  • Python 利用邮件系统完成远程控制电脑的实现(关机、重启等)

    2023-12-23 19:32:54
  • asp之家 网络编程 m.aspxhome.com