Pinia的定义及使用

        从上篇文章Pinia的安装及使用-CSDN博客我们知道如何去安装pinia,这一期我们来学习具体该怎样去初步使用pinia。

一、创建Pinia文件的两种写法

        1、Option Store:

        与 Vue 的选项式 API 类似,我们也可以传入一个带有 state、actions 与 getters 属性的 Option 对象。

export const useCounterStore = defineStore('counter', {
  state: () => ({ count: 0 }),
  getters: {
    double: (state) => state.count * 2,
  },
  actions: {
    increment() {
      this.count++
    },
  },
})

        你可以认为 state 是 store 的数据 (data),getters 是 store 的计算属性 (computed),而 actions 则是方法 (methods)。

        2、Setup Store:

        与 Vue 组合式 API 的 setup 函数 相似,我们可以传入一个函数,该函数定义了一些响应式属性和方法,并且返回一个带有我们想暴露出去的属性和方法的对象。

export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)
  const doubleCount = computed(() => count.value * 2)
  function increment() {
    count.value++
  }

  return { count, doubleCount, increment }
})

        注意,要让 pinia 正确识别 state,你必须在 setup store 中返回 state 的所有属性。  

在 Setup Store 中:

  • ref() 就是 state 属性
  • computed() 就是 getters
  • function() 就是 actions

二、在组件中使用Pinia

        我们需要使用<script setup> 调用 useStore()(或者使用 setup() 函数,像所有的组件那样)

<script setup>
import { useCounterStore } from '@/stores/counter'
// 可以在组件中的任意位置访问 `store` 变量 ✨
const store = useCounterStore()
</script>

三、从 Store 解构

        为了从 store 中提取属性时保持其响应性,你需要使用 storeToRefs()。它将为每一个响应式属性创建引用。当你只使用 store 的状态而不调用任何 action 时,它会非常有用。请注意,你可以直接从 store 中解构 action,因为它们也被绑定到 store 上。

<script setup>
import { storeToRefs } from 'pinia'
const store = useCounterStore()
// `name` 和 `doubleCount` 是响应式的 ref
// 同时通过插件添加的属性也会被提取为 ref
// 并且会跳过所有的 action 或非响应式 (不是 ref 或 reactive) 的属性
const { name, doubleCount } = storeToRefs(store)
// 作为 action 的 increment 可以直接解构
const { increment } = store
</script>

 

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Vue3项目中使用Pinia进行模块化状态管理,可以按照以下步骤进行定义模块化。 1. 首先,在store文件夹中创建一个新的模块文件,例如system.ts,用于定义系统相关的状态和操作。 2. 在该文件中,引入Pinia并创建一个新的store实例,然后定义系统相关的状态和操作。例如: ```typescript import { defineStore } from 'pinia'; export const useSystemStore = defineStore('system', { state: () => ({ // 定义系统相关的状态 // ... }), getters: { // 定义系统相关的getter // ... }, actions: { // 定义系统相关的操作 // ... }, }); ``` 3. 同样地,创建一个新的模块文件,例如user.ts,用于定义用户相关的状态和操作。在该文件中,也需要引入Pinia并创建一个新的store实例,然后定义用户相关的状态和操作。例如: ```typescript import { defineStore } from 'pinia'; export const useUserStore = defineStore('user', { state: () => ({ // 定义用户相关的状态 // ... }), getters: { // 定义用户相关的getter // ... }, actions: { // 定义用户相关的操作 // ... }, }); ``` 4. 在主store文件(通常是index.ts或index.js)中,引入所有的模块文件,并整合它们,最后导出一个store实例。例如: ```typescript import { createPinia } from 'pinia'; import { useSystemStore } from './system'; import { useUserStore } from './user'; export const store = createPinia(); store.use(useSystemStore); store.use(useUserStore); export default store; ``` 5. 现在,你可以在Vue组件中使用store实例来访问和修改模块化的状态,并且调用相关的操作。例如: ```vue <script> import { defineComponent } from 'vue'; import { useSystemStore, useUserStore } from '@/store'; export default defineComponent({ setup() { const systemStore = useSystemStore(); const userStore = useUserStore(); // 使用系统相关的状态和操作 // ... // 使用用户相关的状态和操作 // ... }, }); </script> ``` 通过以上步骤,你可以使用Pinia进行模块化状态管理,将相关的状态和操作封装在不同的模块中,提高代码的可维护性和可扩展性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue3 - Pinia 模块化(详细教程)](https://blog.csdn.net/weixin_44198965/article/details/128114009)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小菜凯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值