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

[vue3] pinia的基本使用

使用Pinia

npm install pinia

store文件里index.js

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

main.js导入并引用

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

pinia的状态管理,不同状态可以区分不同文件
在这里插入图片描述

//定义关于counter的store
import { defineStore } from ‘pinia’//defineStore 是返回一个函数 函数命名最好有use前缀,根据函数来进行下一步操作
const useCounter = defineStore('counter',{state: () => {count:99}
})export default useCounter

调用pinia,获取pinia状态值,导入Counter.js,获取Counter.js里面state.count

<template><div class="home"><h2>Home View</h2><h2>count: {{ counterStore.count }}</h2></div>
</template><script setup>import useCounter from '@/stores/counter';const counterStore = useCounter()</script><style scoped>
</style>

注意:pinia解构出来的state也是可以调用,但会失去响应式,需要toRef或者pinia自带storeToRefs

<template><div class="home"><h2>Home View</h2><h2>count: {{ counterStore.count }}</h2><h2>count: {{ count }}</h2><button @click="incrementCount">count+1</button></div>
</template><script setup>import { toRefs } from 'vue'import { storeToRefs } from 'pinia'import useCounter from '@/stores/counter';const counterStore = useCounter()// const { count } = toRefs(counterStore)const { count } = storeToRefs(counterStore)function incrementCount() {counterStore.count++}</script><style scoped>
</style>

store的核心部分:state,getter,action
(相当于:data、computed、methods)

State

读取和写入state:


const counterStore = useCounter()counterStore.counter++
counterStore.name = 'coderWhy'

重置State reset

const counterStore = useCounter()
conterStore.$reset()

改变State patch

const counterStore = useCounter()counterStore.$patch({counter:100,name:'kobe'
})

替换State

conterStore.$state = {counter:1,name:'why'
}

Getters

Getters相当于Store的计算属性computed:

用defineStore()中的getters属性定义
getters中可以定义接受一个state作为参数的函数

expoer const useCounter = defineStore('counter',{state: () => {counter:100,firstname:'kobe'},getters:{doubleCounter(state){return state.counter *2}}
})

访问当前Store里getters方法

const counterSotre = useCounter()
console.log(counterStore.doublCounter)

使用this来访问当前的store实例中getters

expoer const useCounter = defineStore('counter',{state: () => {counter:100,firstname:'kobe'},getters:{doubleCounter(state){return state.counter *2}doubleCounterAdd(){//this指向storereturn this.doubleCounter +1 }}
})

访问其它store的getters

import useUser from ./userconst userStore = useUser()expoer const useCounter = defineStore('counter',{state: () => {counter:100,firstname:'kobe'},getters:{//调用其它StoredoubleCounterUser(){return this.doubleCounter + userStore.umu}}
})

通过getters可以返回一个函数,可以传参数

expoer const useCounter = defineStore('counter',{state: () => {counter:100,firstname:'kobe'},getters:{//调用其它StoredoubleCounter(state){return function (is) {return state.id + id}}}
})
const StoreConter = useCounter();
//传参
StoreCounter.doublCounter(111)

Actions

Actions 相当于组件中的methods,可以使用defineStore()中的actions属性定义
和getters一样,在action中可以通过this访问整个store实例:

expoer const useCounter = defineStore('counter',{state: () => {counter:100,firstname:'kobe'},getters:{//调用其它StoredoubleCounter(state){return function (is) {return state.id + id}}},actions:{increment(){this.counter++},//传参incrementnum(num){this.counter += num}}
})

Actions执行异步操作 await

actions:{async fetchHome(){//请求const res = await fetch('?????')const data = await res.json()console.log('data',data)return data}
}
cosnt counterStore = useCountercounterStore.fetchHome().then(res => {console.log(res)
})


Vue3-Pinia的基本使用

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

相关文章:

  • 进程和线程详解
  • 《刀锋》读书笔记
  • nginx中的ngx_modules
  • 设计模式之访问者模式
  • Go项目(三)
  • CTK学习:(一)编译CTK
  • 15种NLP数据增强方法总结与对比
  • Python每日一练(20230219)
  • vTESTstudio - VT System CAPL Functions - VT7001
  • 「可信计算」论文初步解读
  • CSDN 算法技能树 蓝桥杯-基础 刷题+思考总结
  • 信小程序点击按钮绘制定制转发分享图
  • Python自动化测试-使用Pandas来高效处理测试数据
  • 语音增强学习路线图Roadmap
  • nginx配置ssl实现https访问
  • JavaScript 语句
  • 将古老的ASP项目转换为PHP初探
  • 数据结构复习(七)模板类封装实现不带头结点的单链表
  • IDEA插件 RestfulTool插件——Restful服务开发辅助工具集
  • 2023年全国最新会计专业技术资格精选真题及答案1
  • Linux 配置RAID组
  • 【2021/推荐/社交网络】Socially-Aware Self-Supervised Tri-Training for Recommendation
  • Django搭建个人博客Blog-Day06
  • DQL 多表查询
  • BUUCTF Reverse xor
  • vite和esbuild/roolup的优缺点
  • 32-Golang中的map
  • LeetCode-384-打乱数组
  • 九龙证券|重大利好!期货公司打新再“解绑”:可直接参与首发网下配售!
  • 信号完整性设计规则之串扰最小化