位置: IT常识 - 正文

Vue3-Pinia的基本使用

编辑:rootadmin
Vue3-Pinia的基本使用 什么是Pinia呢?

推荐整理分享Vue3-Pinia的基本使用,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

Pina开始于大概2019,是一个状态管理的库,用于跨组件、页面进行状态共享(这和Vuex、Redux一样),用起来像组合式API(Composition API)

Pinia和Vuex的区别PInia的最初是为了探索Vuex的下一次迭代会是什么样子,结合了Vuex核心团队讨论中的许多想法;最终,团队意识到Pinia已经实现了Vuex5中大部分内容,所以最终决定用Pinia来替代Vuex;与Vuex相比,Pinia提供了一个更简单的API,具有更少的仪式,提供了Composition-API风格的API更重要的是,与TypeScript一起使用时具有可靠的类型推断支持与Vuex相比,Pinia很多的优势:

比如mutations不再存在:

mutations最初是为devtools集成,但这不在是问题他们经常认为是非常冗长

更友好的TpeScipt支持,Vuex之前对Ts的支持很不友好

不在有modules的嵌套结构

你可以灵活使用每一个store,他们是通过扁平化的方式来相互使用的;

不在有命名空间的概念,不在需要记住他们的复杂关系

如何使用Pinia

1、安装Pinia

yarn add pinianpm install pinia

2、创建pinia文件

store文件里index.js

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

3、main.js导入并引用

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

4、pinia的状态管理,不同状态可以区分不同文件

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

5、调用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>Vue3-Pinia的基本使用

注意: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是store的核心部分,因为store是用来帮助我们管理状态

操作State

读取和写入state:

默认情况下,可以通过store实例访问状态来直接读取和写入状态;

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

重置State: 可以调用store上的$reset()方法将状态重置到其初始值

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

改变State

除了直接用store.counter++修改store,还可以调用$patch

它允许您使用部分‘state’对象同时应该多个修改

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

替换State 可以通过将其$state属性设置为新对象替换Store的整个状态

conterStore.$state = {counter:1,name:'why'}认识和定义Getters

Getters相当于Store的计算属性:

它们可用defineStore()中的getters属性定义getters中可以定义接受一个state作为参数的函数expoer const useCounter = defineStore('counter',{state: () => {counter:100,firstname:'kobe'},getters:{doubleCounter(state){return state.counter *2}}})

访问Store里getters方法

访问当前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属性定义

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}}})

和getters一样,在action中可以通过this访问整个store实例:

function increment(){//调用counterStore.increment()}function incrementnum(){counterStore.increment(10)}Actions执行异步操作:

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)})
本文链接地址:https://www.jiuchutong.com/zhishi/299275.html 转载请保留说明!

上一篇:前端开发是做什么的?工作职责(前端开发做什么副业)

下一篇:Vue 3 介绍

  • 个人独资企业要承担无限责任吗
  • 资产处置损益影响所有者权益总额吗
  • 银行转账支付计入什么科目
  • 商品进销差价在贷方代表什么
  • 文化事业建设费怎么申报
  • 押金收不回来没钱怎么办
  • 利润表中的其他业务利润包括哪些
  • 小企业销售材料计入什么科目借方记什么
  • 房地产企业拆迁补偿契税政策
  • 物业公司收取电损费合法吗
  • 支付宝怎么开个人增值税发票
  • 报关单报关没做收入怎么办
  • 多计提的工资怎么处理?
  • 已认证未抵扣的进项税如何报税
  • 非正常损失的购进货物进项税不能抵扣
  • 跨行发报
  • 上月已认证的发票发现错误怎么办
  • 一般纳税人如何纳税申报
  • 对公账户注销需要本人吗
  • 航道疏浚服务属于什么服务
  • 定金转为货款金额需要特别约定吗?
  • 公路通行费抵扣进项税
  • 施工现场应建立什么
  • 贷款利息支出属于
  • 食堂伙食费需要开票吗
  • 未开票收入计入预收账款
  • 委托代销商品税法规定
  • 对外支付需要缴纳增值税吗
  • vite首次打开界面加载慢问题/解决
  • 苹果发布macOS更新
  • win10任务栏隐藏正在运行的程序
  • 不动产出租管理办法
  • 前端静态页面
  • 非上市公司股票期权个人所得税
  • pytorch如何搭建神经网络
  • opencv 边缘
  • 腾讯一面问什么
  • 社保新参统委托代发银行
  • 分公司二季度安全生产分析会内容
  • 应交税费中印花税是什么
  • 企业补助怎么做会计分录
  • 装货费用
  • 优先股股息必须支付吗
  • 图文详解汽车坐垫安装方法
  • 上一年的成本没入账怎么做
  • 增加固定资产原值50%以上
  • 专利年费的滞纳金怎么做账
  • 中国移动发票抬头开错了可以重开吗
  • 经营租赁筹建期怎么计算
  • 购车买的保险分别是什么
  • 银行代扣出口快递费用
  • 进销存的会计处理实务
  • 库存现金存入银行是什么凭证
  • 商品验收入库时怎么做账
  • 公司缴纳社保如何转为个人缴纳
  • 商业医疗保险的缺点
  • 工会经费计提比例是2%还是0.8%
  • 其他业务收入冲减应付账款
  • 无形资产计提折旧可以转回吗
  • 税务会计应该设什么岗位
  • sql server2008启动
  • mac怎么看文件
  • vmware虚拟机不能用桥接模式
  • linux使用场合
  • 批处理在windows中的典型应用
  • windows8笔记本电脑
  • centos ohmyzsh
  • win101903更新安装失败
  • opengl编程实例
  • vue-cli作用
  • linux shell命令的返回值
  • javascript怎么学
  • unity中事件分发系统 EventDispatcher
  • python操作db2数据库
  • 公职律师
  • 银行存款利息收入现金流量表计入哪
  • 社保征管职责是什么
  • 查册证明怎么自动生成
  • 个体工商户如何注销
  • 新一轮税制改革的背景是什么
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设