Vue3 状态管理 - Pinia

1. 什么是Pinia

Pinia 是 Vue 的专属的最新状态管理库 ,是 Vuex 状态管理工具的替代品

  1. 提供更加简单的APl(去掉了mutation,Pinia 中对state数据的修改可以直接通过action,Vuex中则是通过mutation)
  2. 提供符合组合式风格的API(和Vue3新语法统一)
  3. 去掉了modules的概念,每一个store都是一个独立的模块
  4. 配合TypeScript更加友好,提供可靠的类型推断

2. 手动添加Pinia到Vue项目

后面在实际开发项目的时候,Pinia可以在项目创建时自动添加,现在我们初次学习,从零开始:

  1. 使用 Vite 创建一个空的 Vue3项目
npm init vite@latest
  1. 安装pinia
npm install pinia
  1. 使用pinia
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'

const pinia = createPinia()
const app = createApp(App)

app.use(pinia)
app.mount('#app')

3. Pinia基础使用

  1. 定义store
  2. 组件使用store

定义store(state+action)store/counter.js

store/counter.js

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

export const useCounterStore = defineStore('counter', () => {
  //数据 (state)
  const count = ref(0)

  //修改数据的方法 (action)
  const increment = () => {
    count.value++
  }

  const sub = () => {
    count.value--
  }

  // getter,用computed实现
  const doubleCount = computed(() => count.value * 2 )
  // 以对象形式返回
  return { count, increment, sub, doubleCount}
})

Son1.vue

<script setup>
  defineOptions({
    name: 'Son1Index'
  })
  // 1. 导入`useCounterStore`方法
  import { useCounterStore } from '@/stores/counter'
  // 2. 执行方法得到counterStore对象
  const counterStore = useCounterStore()
</script>

<template>
  <div>
    我是Son1.vue {{ counterStore.count }}  <button @click="counterStore.increment">+</button>
  </div>
</template>

<style>

</style>

4. getters实现

Pinia中的 getters 直接使用 computed函数 进行模拟, 组件中需要使用需要把 getters return出去

const count = ref(0)
//getter
const doubleCount = computed(() => count.value *2)

5. action异步实现

方式:异步action函数的写法和组件中获取异步数据的写法完全一致

  • 接口地址:http://geek.itheima.net/v1_0/channels

  • 请求方式:get

  • 请求参数:无

//异步action
const getList = async () => {
	const res = await axios.request<接口数据类型>({})
}

6. storeToRefs工具函数

使用storeToRefs函数可以辅助保持数据(state + getter)的响应式解构

//响应式丢失,视图不再更新
const { count, doubleCount } = counterStore

//保持数据响应式
const { count, doubleCount } = storeToRefs(counterStore)

数据的解构需要通过storeToRefs()才可以保持数据响应式,如果直接解构则是拷贝一份给解构后的数据,如果是方法的解构则直接解构即可,不需要加上storeToRefs()

7. Pinia的调试

Vue官方的 dev-tools 调试工具 对 Pinia直接支持,可以直接进行调试

8. Pinia持久化插件

官方文档:https://prazdevs.github.io/pinia-plugin-persistedstate/zh/
pinia持久化作用等同于localstore

  1. 安装插件 pinia-plugin-persistedstate
npm i pinia-plugin-persistedstate
  1. 使用 main.js
// 导入持久化插件,该插件是给pinia使用的
import persist from 'pinia-plugin-persistedstate'
...
app.use(createPinia().use(persist))
  1. 配置 store/counter.js
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

export const useCounterStore = defineStore('counter', () => {
  ...
  return {
    count,
    doubleCount,
    increment
  }
}, {
  persist: true
})
  1. 其他配置,看官网文档即可

    如果不打算持久化整个counter,可以指定具体的属性,

    persist:{
        paths: ['count','doubleCount']
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Pinia 是一个基于 Vue 3 的状态管理库,可以让你更方便地管理应用程序的状态。下面是一个简单的例子,展示如何使用 Vue Pinia: 1. 安装 Vue Pinia ```bash npm install @pinia/vue3 ``` 2. 创建一个 store ```javascript import { defineStore } from '@pinia/vue3' export const useCounterStore = defineStore({ id: 'counter', state: () => ({ count: 0 }), actions: { increment() { this.count++ } } }) ``` 上面的代码创建了一个名为 `useCounterStore` 的 store,用于管理一个名为 `count` 的状态,并且提供了一个名为 `increment` 的 action,用于增加 `count` 的值。 3. 在组件中使用 store ```vue <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> </div> </template> <script> import { useCounterStore } from './store' export default { setup() { const counterStore = useCounterStore() return { count: counterStore.count, increment: counterStore.increment } } } </script> ``` 在组件中使用 `useCounterStore`,并且将 `count` 和 `increment` 绑定到组件的模板中。 4. 在应用程序中注册 store ```javascript import { createApp } from 'vue' import App from './App.vue' import { createPinia } from '@pinia/vue3' import { useCounterStore } from './store' const app = createApp(App) const pinia = createPinia() pinia.useStore(useCounterStore) app.use(pinia) app.mount('#app') ``` 在应用程序中注册 `useCounterStore`,并将其添加到 `pinia` 实例中。 以上就是使用 Vue Pinia 的一些基本步骤。通过使用 Vue Pinia,你可以轻松地管理应用程序的状态,提高开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值