使用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指向store
return this.doubleCounter +1
}
}
})
访问其它store的getters
import useUser from ./user
const userStore = useUser()
expoer const useCounter = defineStore('counter',{
state: () => {
counter:100,
firstname:'kobe'
},
getters:{
//调用其它Store
doubleCounterUser(){
return this.doubleCounter + userStore.umu
}
}
})
通过getters可以返回一个函数,可以传参数
expoer const useCounter = defineStore('counter',{
state: () => {
counter:100,
firstname:'kobe'
},
getters:{
//调用其它Store
doubleCounter(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:{
//调用其它Store
doubleCounter(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 = useCounter
counterStore.fetchHome().then(res => {
console.log(res)
})
Vue3-Pinia的基本使用