Vue3状态管理pinia

本文详细介绍了Pinia,Vue3.x的状态管理库,包括其基本概念、安装使用、创建和使用state、action以及getter的方法。Pinia简化了状态管理,无需像Vuex那样使用mutations,支持直接修改state,并提供了批量更新状态的$patch方法。同时,文章还展示了如何在组件中使用Pinia,以及action和getter的用法。
摘要由CSDN通过智能技术生成


一、Pinia是什么?

pinia是vue3.x新出的状态管理,它包括state,getter,action。

二、使用步骤

1.安装使用Pinia

代码如下(示例):

yarn add pinia
# or with npm
npm install pinia

2. main.js引入

代码如下(示例):

import { createPinia } from 'pinia'

app.use(createPinia())

3. main.js引入

代码如下(示例):

import { createPinia } from 'pinia'

app.use(createPinia())

4. 根目录新建store/index.js中写入

代码如下(示例):

import { defineStore } from 'pinia'

export const useStore = defineStore('storeId', {
  state: () => {
    return {
      counter: 0,
    }
  },
  getters:{},
  actions:{}
})

5. 组件中使用

代码如下(示例):

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

三. State

1. Pinia定义state数据

代码如下(示例):

import { defineStore } from 'pinia'

export const useStore = defineStore('storeId', {
  state: () => {
    return {
      counter: 0,
      name: 'Eduardo',
      isAdmin: true,
    }
  },
  getters:{},
  actions:{}
})

2. 组件使用pinia的state数据

代码如下(示例):

import { createPinia } from 'pinia'

app.use(createPinia())

2. 组件中使用

代码如下(示例):

<template>
	<div>
		<h1>A组件</h1>
		{{ name }}
	</div>
</template>

<script setup>
import { useStore } from '../store'
const store = useStore();
let { name } = store;
</script>

3.组件修改pinia的state数据

本身pinia可以直接修改state数据,无需像vuex一样通过mutations才可以修改,
但是上面写的let { name } = store;这种解构是不可以的,所以要换解构的方式。
<template>
	<div>
		<h1>A组件</h1>
		{{ name }}
		<button @click='btn'>按钮</button>
	</div>
</template>
<script setup>
import { storeToRefs } from 'pinia'
import { useStore } from '../store'
const store = useStore();
let { name }  = storeToRefs(store);
const btn = ()=>{
	name.value = '123';
}
</script>

4.如果state数据需要批量更新

<template>
	<div>
		<h1>A组件</h1>
		{{ name }}
		{{ counter }}
		{{ arr }}
		<button @click='btn'>按钮</button>
	</div>
</template>
<script setup>
import { storeToRefs } from 'pinia'
import { useStore } from '../store'
const store = useStore();
let { name,counter,arr }  = storeToRefs(store);
const btn = ()=>{
	//批量更新
	store.$patch(state=>{
		state.counter++;
		state.arr.push(4);
		state.name = '456';
	})
}
</script>

使用$patch进行批量更新


四.action

actions就比较简单了,写入方法,比如我们可以让state中的某一个值+=,而且传入参数
import { defineStore } from 'pinia'

export const useStore = defineStore('storeId', {
  state: () => {
    return {
      counter: 0
    }
  },
  getters:{},
  actions:{
  	changeCounter( val ){
  		this.counter += val;
  	}
  }
})
<template>
	<div>
		<h1>A组件</h1>
		{{ counter }}
		<button @click='add'>10</button>
	</div>
</template>
<script setup>
import { storeToRefs } from 'pinia'
import { useStore } from '../store'
const store = useStore();
let { counter }  = storeToRefs(store);
const add = ()=>{
	store.changeCounter(10);
}
</script>

五. getter

getters和vuex的getters几乎类似,也是有缓存的机制
import { defineStore } from 'pinia'

export const useStore = defineStore('storeId', {
  state: () => {
    return {
      counter: 0,
    }
  },
  getters:{
  	counterPar(  ){
  		console.log(111);
  		return this.counter + 100;
  	}
  },
  actions:{}
})
<template>
	<div>
		{{ counterPar }}
		{{ counterPar }}
		{{ counterPar }}
		<h1>A组件</h1>
		{{ counter }}
	</div>
</template>
<script setup>
import { storeToRefs } from 'pinia'
import { useStore } from '../store'
const store = useStore();
let { counter, counterPar }  = storeToRefs(store);
</script>

总结

  • 支持选项式api和组合式api写法
  • pinia没有mutations,只有:state、getters、actions
  • pinia分模块不需要modules(之前vuex分模块需要modules)
  • TypeScript支持很好
  • 自动化代码拆分
  • pinia体积更小(性能更好)
  • 可以直接修改状态,不需要和vuex一样,需要mutations才可以修改
  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 3 是一个流行的 JavaScript 框架,而 Pinia 是一个基于 Vue 3 的状态管理库。要在 Vue 3 中使用 Pinia,你需要进行以下步骤: 1. 安装 Pinia:你可以使用 npm 或者 yarn 安装 Pinia。 使用 npm: ``` npm install pinia ``` 使用 yarn: ``` yarn add pinia ``` 2. 创建 Pinia 实例:在你的应用程序的入口文件中,创建一个 Pinia 实例。 ```javascript import { createPinia } from 'pinia'; const pinia = createPinia(); ``` 3. 配置 Vue 3:在应用程序的入口文件中,配置 Vue 3 以使用 Pinia。 ```javascript import { createApp } from 'vue'; import App from './App.vue'; import { pinia } from './pinia'; const app = createApp(App); app.use(pinia); app.mount('#app'); ``` 4. 创建 store:在你的项目中创建一个 store,它将承载你的状态和操作。 ```javascript import { defineStore } from 'pinia'; export const useStore = defineStore('store', { state: () => ({ count: 0, }), actions: { increment() { this.count++; }, }, }); ``` 5. 在组件中使用 store:在你的组件中使用创建的 store。 ```vue <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> </div> </template> <script> import { useStore } from './store'; export default { setup() { const store = useStore(); return { count: store.count, increment: store.increment, }; }, }; </script> ``` 这就是如何在 Vue 3 中使用 Pinia。通过使用 Pinia,你可以更方便地管理应用程序的状态。记得引入相关的依赖,然后按照上述步骤进行操作即可。希望对你有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值