当前位置: 首页 > news >正文

【vue 引入pinia与pinia的详细使用】

vue引入pinia与使用

    • 安装
    • 引入
    • 使用
      • 定义 store
      • 在组件中使用 store
      • 在插件中使用 store
      • 配置 store
    • 总结

Pinia 是一个用于 Vue 3 的状态管理库,其设计目标是提供一个简单、一致的 API 和强类型支持。下面介绍如何引入 Pinia 并使用它。

安装

npm install pinia

引入

在 main.js 中引入 Pinia:

import { createApp } from 'vue'
import App from './App.vue'
import { createPinia } from 'pinia'const app = createApp(App)const pinia = createPinia()
app.use(pinia)app.mount('#app')

这里我们用 createPinia 方法创建了一个新的 Pinia 实例,并通过 app.use 方法将其注册到 Vue 应用实例中。

使用

定义 store

在 Pinia 中,我们通过 defineStore 方法来定义一个 store:

import { defineStore } from 'pinia'export const useCounterStore = defineStore({id: 'counter',state: () => ({ count: 0 }),actions: {increment() {this.count++},decrement() {this.count--},},
})

在这个例子中,我们定义了一个名为 useCounterStore 的 store,其中包含一个状态属性 count 和两个 action 方法 incrementdecrement

在组件中使用 store

<template><div><p>Count: {{ counter.count }}</p><button @click="increment">Increment</button><button @click="decrement">Decrement</button></div>
</template><script>
import { useCounterStore } from './store'export default {setup() {const counter = useCounterStore()function increment() {counter.increment()}function decrement() {counter.decrement()}return {counter,increment,decrement,}},
}
</script>

在组件中,我们通过 useCounterStore() 方法获取 useCounterStore 的实例,并通过它来访问状态属性和 action 方法。

在插件中使用 store

如果你需要在插件中使用 store,那么可以通过 useStore 方法来获取 store 实例:

import { useCounterStore } from './store'export default {install(app, options) {app.provide('counterStore', useCounterStore())// ...}
}

在这个例子中,我们将 useCounterStore() 的返回值提供给了 Vue 的 provide 方法,以便在插件中进行访问。

配置 store

defineStore 方法还支持可选的 actionsgettersmutations 配置项,以支持更加灵活的状态管理模式。

import { defineStore } from 'pinia'export const useCounterStore = defineStore({id: 'counter',state: () => ({ count: 0 }),getters: {doubleCount() {return this.count * 2},},actions: {increment() {this.count++},decrement() {this.count--},},mutations: {reset() {this.count = 0},},
})

在这个例子中,我们定义了一个名为 doubleCount 的 getter 和一个名为 reset 的 mutation 方法。Getter 可以用于派生计算属性,Mutation 可以用于同步修改状态。

总结

以上就是如何引入和使用 Pinia 的详细介绍。相比 Vuex 和其他类似的状态管理库,Pinia 更加轻量化且易于使用,适合中小型 Vue 项目的状态管理。

http://www.lryc.cn/news/136376.html

相关文章:

  • USACO18DEC Fine Dining G
  • fckeditor编辑器的两种使用方法
  • 数据结构,查找算法(二分,分块,哈希)
  • C++(Qt)软件调试---gdb调试入门用法(12)
  • shell和Python 两种方法分别画 iostat的监控图
  • 设计模式(9)建造者模式
  • PHP 创业感悟交流平台系统mysql数据库web结构apache计算机软件工程网页wamp
  • 工作流程引擎之flowable(集成springboot)
  • leetcode54. 螺旋矩阵(java)
  • go gorm 查询
  • Flutter GetXController 动态Tabbar 报错问题
  • Redis(缓存预热,缓存雪崩,缓存击穿,缓存穿透)
  • UE4/5Niagara粒子特效学习(使用UE5.1,适合新手)
  • from moduleA import * 语句 和import moduleA 的区别
  • 【leetcode 力扣刷题】交换链表中的节点
  • 学会Mybatis框架:让你的代码更具灵活性、可维护性、安全性和高效性【二.动态SQL】
  • Oracle 中 ROWNUM 使用问题记录
  • MySQL数据库:内置函数
  • 【C++杂货铺】探索string的底层实现
  • c++ day1
  • 变动的Python爬虫实现
  • mybatis-plus--配置-(sql)日志输出-自动填充-分页-多数据源-逻辑删除
  • 数据API服务管理功能:解放数据潜力,提升业务效率
  • 云南森林火灾vr消防模拟安全演练系统训练消防员火灾和事故的适应和应对能力
  • (6)(6.2) 任务命令
  • 【consul】
  • Electron环境搭建
  • MinIO线上扩容实战
  • 【微服务】微服务的概论
  • 基于Jenkins自动打包并部署docker环境