Use of Pinia

Steps for usage:

  • Install
yarn add pinia
# or
npm i pinia
  • Import, instantiate, use as a plug-in, and use the same routine as other plug-ins

For better functional maintenance, import the instance pinia under the store file

store/index.ts

Download the data storage persistence plug-in

npm i pinia-plugin-persistedstate
/**
 * 功能:维护pinia
 * 创建的pinia仓库从这里统一导出
 */
import { createPinia } from 'pinia'
// pinia-plugin-persistedstate 持久化存储插件
import persist from 'pinia-plugin-persistedstate'

const pinia = createPinia()

// vue使用pinia插件 .use(pinia里面的插件-持久化存储)
pinia.use(persist)

export default pinia

// 完整写法
// import { useUserStore } from './user'
// export { useUserStore }
// 统一导出:和上面两行完整写法是等价的
export * from './user'
export * from './modules/consult'

main.ts 

import { createApp } from 'vue'

import App from './App.vue'
// 引入路由
import router from './router'
// 引入仓库
import pinia from './stores'
// 引入vant样式,(在全局样式前导入方便自定义样式的覆盖)
import 'vant/lib/index.css'
// 引入全局样式
import './styles/main.scss'
import 'virtual:svg-icons-register'

const app = createApp(App)
app.use(router)

app.use(pinia)
app.mount('#app')
  • Create warehouse & use warehouse

store/user.ts

import { defineStore } from "pinia"
import { computed, ref } from "vue"

// cp-user 持久化存储仓库的名称
export const useUserStore= defineStore("cp-user", () => {
    // 1.用户信息
    const user = ref<User>()
    // 2.修改用户信息
    const setUser = (u: User) => {
      user.value = u
    }
    // 3.删除用户信息
    const delUser = () => {
      user.value = undefined
    }
    return {
       user,
       setUser,
       delUser 
    },
    // 开启持久化,使用本地存储,默认使用localStorage
    { persist: true }
     // {
     //   persist: {
     //     key: 'store-key',
     //     storage: window.sessionStorage,
     //     paths: ['nested.data'],
     //     beforeRestore: (context) => {
     //       console.log('Before hydration...')
     //     },
     //     afterRestore: (context) => {
     //       console.log('After hydration...')
     //     }
     //   }
     // }
})

 The home.vue page introduces store data

<script setup lang="ts">

// 引入定义的store仓库 useUserStore
import { useUserStore} from "./store/counter"

// store中有状态和函数
const store = useCounterStore()

console.log(store.user,store.setUser,store.delUser)

</script>

The use of storeToRefs 

Use storeToRefs to solve the problem of deconstructed warehouse state loss responsiveness

question:

  • When we want to deconstruct the data provided by the store, we find that the data is not responsive.

remember:

  • When learning the responsive data created by the vue combined API, use  toRefs the responsive data that maintains the structure

plan:

  • Use  storeToRefs to solve the problem of deconstructing warehouse state loss responsiveness

code:

  • for state management
  // state
  const count = ref(100)
  // getters
  const doubleCount = computed(() => count.value * 2)
  // mutations
  const update = () => count.value++
  // actions
  const asyncUpdate = () => {
    setTimeout(() => {
      count.value++
    }, 1000)
  }
  return { count, doubleCount, update, asyncUpdate }
<template>
  APP {
   
   { store.count }} {
   
   { store.doubleCount }}
  <button @click="store.update()">count++</button>
  <button @click="store.asyncUpdate()">async update</button>
</template>
import { storeToRefs } from 'pinia'

const store = useCounterStore()
const { count, doubleCount } = storeToRefs(store)

summary:

  • When you want to deconstruct the corresponding state from the store, you need to use storeToRefs

Guess you like

Origin blog.csdn.net/luoxiaonuan_hi/article/details/131437253