pinia使用

482 阅读2分钟

pinia的基本使用

  • pinia核心概念
    • state:状态
    • actions:修改状态(包括同步和异步,pinia中没有mutations)
    • getters:计算属性
    • pinia模块化 vuex只能有一个根级别状态,pinia直接就可以定义多个根级别装态。

基本使用与state

  • (1)安装
npm i pinia
# or
yarn add pinia
  • (2)在main.js中挂载pinia
import App from './App.vue'
import { createPinia } from 'pinia'
const pinia = createPinia()
createApp(App).use(pinia).mount('#app')
  • (3)新建文件store/counter.js
import { defineStore } from 'pinia'
// 创建store,命名规则: useXxxxStore
// 参数1:store的唯一表示
// 参数2:对象,可以提供state actions getters
const useCounterStore = defineStore('counter', {
  state: () => {
    return {
      count: 0,
    }
  },
  getters: {
   
  },
  actions: {
 
  },
})
export default useCounterStore
  • (4)在组件中使用
<script setup>
import useCounterStore from './store/counter'

const counter = useCounterStore()
</script>

<template>
  <h1>根组件---{{ counter.count }}</h1>
</template>

<style></style>

actions的使用

在pinia中没有mutations,只有actions,不管是同步还是异步的代码,都可以在actions中完成。

  • (1)在actions中提供方法并且修改数据
import { defineStore } from 'pinia'
// 1. 创建store
// 参数1:store的唯一表示
// 参数2:对象,可以提供state actions getters
const useCounterStore = defineStore('counter', {
  state: () => {
    return {
      count: 0,
    }
  },
  actions: {
    incrementAsync() {
      setTimeout(() => {
        this.count++
      }, 1000)
    },
  },
})

export default useCounterStore
  • (2)在组件中使用
<script setup>
import useCounterStore from './store/counter'
const counter = useCounterStore()
// 调用actions方法修改state中的值
counter.incrementAsync()
</script>

getters的使用

pinia中的getters和vuex中的基本是一样的,也带有缓存功能

  • (1)在getters中提供计算属性
import { defineStore } from 'pinia'
// 1. 创建store
// 参数1:store的唯一表示
// 参数2:对象,可以提供state actions getters
const useCounterStore = defineStore('counter', {
  state: () => {
    return {
      count: 0,
    }
  },
  getters: {
    double() {
      return this.count * 2
    },
  },
  actions: {
    incrementAsync() {
      setTimeout(() => {
        this.count++
      }, 1000)
    },
  },
})

export default useCounterStore
  • (2)在组件中使用
<script setup>
import useCounterStore from './store/counter'

const counter = useCounterStore()
</script>

<template>
  <h1>根组件---{{ counter.count }}</h1>
  <h1>{{counter.double}}</h1>
</template>

storeToRefs的使用

如果直接从pinia中结构数据,会丢失响应式,使用storeToRefs可以保证结构出来的数据也是响应式的。

<script setup>
import { storeToRefs } from 'pinia'
import useCounterStore from './store/counter'

const counter = useCounterStore()
// 如果直接从pinia中解构数据,会丢失响应式===错误写法
const { count, double } = counter

// 使用storeToRefs可以保证解构出来的数据也是响应式的
const { count, double } = storeToRefs(counter)
</script>

pinia模块化

在复杂项目中,不可能吧多个模块的数据都定义到一个store中,一般来说一个模块对应一个对应store,最后通过一个根store进行整合。

  • (1)新建一个store/user.js文件
import { defineStore } from 'pinia'

const useUserStore = defineStore('user', {
  state: () => {
    return {
      name: 'zs',
      age: 100,
    }
  },
})

export default useUserStore
  • (2)新建store/index.js文件
import useUserStore from './user'
import useCounterStore from './counter'

// 统一导出useStore方法
export default function useStore() {
  return {
    user: useUserStore(),
    counter: useCounterStore(),
  }
}
  • (3) 在组件中使用
<script setup>
import { storeToRefs } from 'pinia'
import useStore from './store'
const { counter } = useStore()

// 使用storeToRefs可以保证解构出来的数据也是响应式的
const { count, double } = storeToRefs(counter)
</script>