pinia状态管理

状态管理Pinia

一、简介

Pinia是一个相对较新的状态管理库,它是为Vue 3设计的,但也可以在Vue 2项目中使用。Pinia是目前官方维护和推荐的Vue状态管理工具。Pinia提供了一种更现代的方式来管理Vue.js应用程序的状态,采用了Composition API的思想。以下是一个简单的Pinia示例:

// stores/counter.js
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
 state: () => {
   return { count: 0 }
 },
 // 也可以这样定义
 // state: () => ({ count: 0 })
 actions: {
   increment() {
     this.count++
   },
 },
})

然后你就可以在一个组件中使用该 store 了:

<script setup>
import { useCounterStore } from '@/stores/counter'
const counter = useCounterStore()
counter.count++
// 自动补全! ✨
counter.$patch({ count: counter.count + 1 })
// 或使用 action 代替
counter.increment()
</script>
<template>
  <!-- 直接从 store 中访问 state -->
  <div>Current Count: {{ counter.count }}</div>
</template>

二、安装Pinia

用你喜欢的包管理器安装 pinia:

yarn add pinia
# 或者使用 npm
npm install pinia

创建一个 pinia 实例 (根 store) 并将其传递给应用:

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')

三、核心概念

1.State

在 Pinia 中,state 被定义为一个返回初始状态的函数。

  • 访问 state
const store = useStore()
store.count++
  • 重置 state
const store = useStore()
store.$reset()
  • 变更 state
store.$patch({
  count: store.count + 1,
  age: 120,
  name: 'DIO',
})
store.$patch((state) => {
  state.items.push({ name: 'shoes', quantity: 1 })
  state.hasChanged = true
})
  • 替换 state
// 这实际上并没有替换`$state`
store.$state = { count: 24 }
// 在它内部调用 `$patch()`:
store.$patch({ count: 24 })
2.Getter

Getter 完全等同于 store 的 state 的计算值。可以通过 defineStore() 中的 getters 属性来定义它们。

  • 访问其他 getter
export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0,
  }),
  getters: {
    // 类型是自动推断出来的,因为我们没有使用 `this`
    doubleCount: (state) => state.count * 2,
    // 这里我们需要自己添加类型(在 JS 中使用 JSDoc)
    // 可以用 this 来引用 getter
    /**
     * 返回 count 的值乘以 2 加 1
     *
     * @returns {number}
     */
    doubleCountPlusOne() {
      // 自动补全 ✨
      return this.doubleCount + 1
    },
  },
})
  • 向 getter 传递参数
export const useUserListStore = defineStore('userList', {
  getters: {
    getUserById: (state) => {
      return (userId) => state.users.find((user) => user.id === userId)
    },
  },
})

并在组件中使用:

<script setup>
import { useUserListStore } from './store'
const userList = useUserListStore()
const { getUserById } = storeToRefs(userList)
// 请注意,你需要使用 `getUserById.value` 来访问
// <script setup> 中的函数
</script>

<template>
  <p>User 2: {{ getUserById(2) }}</p>
</template>
  • 访问其他 store 的 getter
import { useOtherStore } from './other-store'

export const useStore = defineStore('main', {
  state: () => ({
    // ...
  }),
  getters: {
    otherGetter(state) {
      const otherStore = useOtherStore()
      return state.localData + otherStore.data
    },
  },
})
3.Action

Action 相当于组件中的 method。它们可以通过 defineStore() 中的 actions 属性来定义,并且它们也是定义业务逻辑的完美选择。

export const useCounterStore = defineStore('main', {
  state: () => ({
    count: 0,
  }),
  actions: {
    increment() {
      this.count++
    },
    randomizeCounter() {
      this.count = Math.round(100 * Math.random())
    },
  },
})

Action 可以像函数或者通常意义上的方法一样被调用:

<script setup>
const store = useCounterStore()
// 将 action 作为 store 的方法进行调用
store.randomizeCounter()
</script>
<template>
  <!-- 即使在模板中也可以 -->
  <button @click="store.randomizeCounter()">Randomize</button>
</template>
Pinia是一个基于Vue.js的状态管理库,特别是为Vue 3设计,同时兼容Vue 2,它为Vue开发者提供了简洁高效的状态管理解决方案,可用于跨组件、页面进行状态共享[^1][^2][^3]。 在使用方法上,安装Pinia可通过`yarn add pinia`或`npm install pinia` 。在Vue 3中使用示例如下: ```javascript import { createApp } from 'vue' import App from './App.vue' import {createPinia} from 'pinia' const store = createPinia() let app = createApp(App) app.use(store) app.mount('#app') ``` 在Vue 2中使用示例如下: ```javascript import { createPinia, PiniaVuePlugin } from 'pinia' Vue.use(PiniaVuePlugin) const pinia = createPinia() new Vue({ el: '#app', // other options... // ... // note the same `pinia` instance can be used across multiple Vue apps on // the same page pinia, }) ``` [^5] Pinia具有以下特点: - 简单性:设计目标是提高开发效率和用户体验,更加简单和简洁[^4]。 - 模块化:允许将应用程序的状态划分为独立的"stores",每个store都有自己的状态、actions和getters,使代码更加组织有序[^4]。 - 响应式:利用Vue.js内置的响应式系统,状态的变化会自动反映在UI上[^4]。 - TypeScript支持:对TypeScript有良好的支持,适合使用TypeScript的项目[^4]。 - 组合式API:被设计为与Vue.js的组合式API配合使用,提供更灵活的状态管理方式[^4]。 - Devtools集成:与Vue.js Devtools扩展程序集成,提供强大的调试体验[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值