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

vue之Pinia

定义 Store | Pinia

开发文档

1.什么是Pinaia

Pinia 是 Vue 的专属状态管理库,它允许你跨组件或页面共享状态。

2.理解Pinaia核心概念

定义Store

在深入研究核心概念之前,我们得知道 Store 是用 defineStore() 定义的,它的第一个参数要求是一个独一无二的名字:

import { defineStore } from 'pinia'// 你可以对 `defineStore()` 的返回值进行任意命名,但最好使用 store 的名字,同时以 `use` 开头且以 `Store` 结尾。(比如 `useUserStore`,`useCartStore`,`useProductStore`)
// 第一个参数是你的应用中 Store 的唯一 ID。
export const useAlertsStore = defineStore('alerts', {// 其他配置...
})

这个名字 ,也被用作 id ,是必须传入的, Pinia 将用它来连接 store 和 devtools。为了养成习惯性的用法,将返回的函数命名为 use... 是一个符合组合式函数风格的约定。

defineStore() 的第二个参数可接受两类值:Setup 函数或 Option 对象。、

Setup Store 中:

  • ref() 就是 state 属性
  • computed() 就是 getters
  • function() 就是 actions

使用Store

虽然我们前面定义了一个 store,但在我们使用 <script setup> 调用 useStore()(或者使用 setup() 函数,像所有的组件那样) 之前,store 实例是不会被创建的

<script setup>
import { useCounterStore } from '@/stores/counter'
// 可以在组件中的任意位置访问 `store` 变量 ✨
const store = useCounterStore()
</script>

3.状态持久化

方法一 整体添加

main.ts入口文件添加整体持久化方案

import {createPinia} from 'pinia'
const pinia = createPinia()
if(localStorage.getItem("pinia")){
pinia.state.value = JSON.parse(localStorage.getItem("pinia"))
}
watch(pinia.state,state=>{
localStorage.setItem("pinia",JSON.stringify(state))
},{deep:true})
app.use(pinia)

方法二 插件

npm i pinia-plugin-persistedstate --save
import {createPinia} from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)
app.use(pinia)
import {ref, computed, watch} from 'vue'
import {defineStore} from 'pinia'
//counter是状态库id 或名称
export const useCounterStore = defineStore('counter', () => {
//变量 state
const count = ref(0)
//计算属性 getter
const doubleCount = computed(() => count.value * 2)
//方法action
function increment() {
count.value++
}
const user = ref({
name: '',
desc: '一键三连'
})
return {count, doubleCount, increment}
}, {persist: true})

结果添加 ,{persist: true} 就可以持久化了

Pinanad的使用

components/Aa.vue 组件

components/Bb.vue组件

stores/counter.js     选项式的用法

 stores/ token.js     组合式写法

main.js

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

App.vue

<script setup>
import {version as v1, ref} from 'vue'
import Bb from "@/components/Bb.vue";
import Aa from "@/components/Aa.vue";
const v2 = ref('2.1.6')
import {useCounterStore} from './stores/counter'
const c = useCounterStore()
import {useTokenStore} from './stores/token'
const us = useTokenStore()
</script><template>{{ v1 }} {{ v2 }}<h1>App</h1><button @click="us.count++">add</button><button @click="us.increment()">increment</button><button @click="c.count++">add</button><button @click="c.increment()">increment</button><p>{{us.count}} ---{{us.doubleCount}}</p><hr><p>{{ c.count }} -- {{ c.doubleCount }}</p><p>{{}}</p><h1>Aa组件</h1><Aa/><h1>BB组件</h1><Bb/></template><style scoped></style>

count.js

import {computed, ref, watch} from 'vue'
import {defineStore} from 'pinia'
export const useCounterStore = defineStore('counter', () => {const count = ref(0)const doubleCount = computed(() => count.value * 2)function increment() {count.value++}return {count, doubleCount, increment}
})

token.js


import {defineStore} from 'pinia'export const useTokenStore = defineStore('token', {state: () => {return {count: 0,}},getters: {doubleCount: state => state.count * 2},actions: {increment() {this.count++}},
})

Aa.vue

<script setup>
import {useCounterStore} from '../stores/counter.js'const cc = useCounterStore()import {useTokenStore} from '../stores/token'const us = useTokenStore()</script><template><h1>Aaaaaa</h1><p>Aaa : {{ cc.count }} -- {{ cc.doubleCount }}</p><button @click="cc.count+=5">Aa add +=5</button><p>Aaa : {{ us.count }} -- {{ us.doubleCount }}</p><button @click="us.count+=5">Aa add +=5</button>
</template><style scoped></style>

Ba.vue

<script setup>
import {useCounterStore} from '../stores/counter.js'
const cc = useCounterStore()
</script><template>
<h1>Bb</h1>
<p>Bb : {{cc.count}}  -- {{cc.doubleCount}}</p>
</template><style scoped></style>

http://www.lryc.cn/news/175337.html

相关文章:

  • antd-vue 级联选择器默认值不生效解决方案
  • 分享53个Python源码源代码总有一个是你想要的
  • 【每日一题】658. 找到 K 个最接近的元素
  • 并发任务队列(字节青训测试题)
  • Ubuntu 安装Nacos
  • CSS 小球随着椭圆移动
  • 【李沐深度学习笔记】线性代数
  • vuejs - - - - - 递归组件的实现
  • 精准对接促合作:飞讯受邀参加市工信局举办的企业供需对接会
  • 数学建模之遗传算法
  • ISO9001认证常见的不符合项
  • crypto:看我回旋踢
  • Springcloud实战之自研分布式id生成器
  • java 企业工程管理系统软件源码 自主研发 工程行业适用
  • Spring Cloud Alibaba Nacos 2.2.3 (4) - 本地源码编译 调试
  • WKB近似
  • LeetCode算法二叉树—108. 将有序数组转换为二叉搜索树
  • 如何设置 Git 短命令
  • virtualbox无界面打开linux虚拟机的bat脚本,以及idea(代替Xshell)连接linux虚拟机的方法
  • mockito 的 InjectMocks 和 Mock 有什么区别?
  • 网络工程师的爬虫技术之路:跨界电商与游戏领域的探索
  • 【TCP】确认应答 与 超时重传
  • Kubernetes中Pod的扩缩容介绍
  • vue点击pdf文件直接在浏览器中预览文件
  • 通讯网关软件012——利用CommGate X2OPC实现MS SQL数据写入OPC Server
  • ISE_ChipScope Pro的使用
  • 北邮22级信通院数电:Verilog-FPGA(2)modelsim北邮信通专属下载、破解教程
  • 【力扣-每日一题】213. 打家劫舍 II
  • 【PDF】pdf 学习之路
  • 排序算法二 归并排序和快速排序