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

Pinia小菠萝(状态管理器)

Pinia 是一个专为 Vue 3 设计的状态管理库,它借鉴了 Vuex 的一些概念,但更加轻量灵活。下面将详细介绍如何使用 Pinia 状态管理库:

  1. 安装 Pinia

    • 使用 npm:在项目目录下运行npm install pinia
    • 使用 yarn:在项目目录下运行yarn add pinia
  2. 创建 Pinia 实例

    • 在入口文件中引入并创建实例:在项目的入口文件(通常是main.jsmain.ts)中,导入并创建一个 Pinia 实例,然后将其挂载到应用程序上。
import './assets/main.css'import { createApp } from 'vue'
import App from './App.vue'//1.到入createPinia
import { createPinia } from 'pinia'//2.执行方法得到实例
const pinia = createPinia()//3.把pinia实例加入到app应用中createApp(App).use(pinia).mount('#app')

                2.自定义一个组件,该类编写在components/stores/counter.js

// 导入一个方法 defineStoreimport {defineStore} from 'pinia'
import { ref } from 'vue'export const useCounterStore = defineStore('counter',()=>{const count = ref(0)//定义修改数据的方法(active 同步 + 异步)const increate = () =>{count.value++;}//将对象的方法使用return供组件使用return {count,increate}})

在App.vue中使用该组件

<script setup>
//1.导入 use 打头的方法
import { useCounterStore } from './stores/counter';//2.执行方法得到store实例对象
const counterStore = useCounterStore();
</script><template><div><p>Count: {{ counterStore.count }}</p><button @click="counterStore.increate()">Increment</button></div>
</template><style scoped></style>

测试  结果:

getter方法的定义

    //getter的定义const doubeleCount = computed(() =>count.value * 2)//将对象的方法使用return供组件使用return {count,increate,doubeleCount}
<template><div><p>Count: {{ counterStore.count }}</p><p>DoubleCount:{{ counterStore.doubeleCount }}</p></div>
</template>

效果:

异步action

首先安装依赖 npm install axios

其次,在组件中导出调用异步的方法getList

// 导入一个方法 defineStore
import {defineStore} from 'pinia'
import { ref,computed } from 'vue'
import axios from 'axios'const API_URL = 'https://jsonplaceholder.typicode.com/todos'export const useCounterStore = defineStore('counter',()=>{//定义异步actionconst list = ref([])const getlist = async () =>{const res = await axios.get(API_URL)list.value = res.dataconsole.log(list.value);}//将对象的方法使用return供组件使用return {getlist,list}
})

在组件中,引入并且调用方法即可

<template><div><p>Count: {{ counterStore.count }}</p><p>DoubleCount:{{ counterStore.doubeleCount }}</p><button @click="counterStore.increate()">Increment</button><button @click="counterStore.getlist()">getListData</button><hr><ol v-for="item in counterStore.list" :key="item.id"><li >{{ item.title }}</li><li >{{ item.completed }}</li><li >{{ item.userId }}</li></ol></div>
</template>

效果:

初始界面:

点击getListData按钮后:

storeToRefs解构赋值

首先,引入相关依赖 storeToRefs,然后将响应式对象进行解构赋值

( 方法不需要使用storeToRefs解构赋值 切记!!!)

//针对响应式对象的解构赋值
const { count, doubeleCount, list } = storeToRefs(counterStore);
//对于方法的解构赋值(不需要使用storeToRefs)
const { increate,getlist } = counterStore;

此外,方法解构赋值,在调用方法时,括号加不加都行

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

相关文章:

  • Python知识点:基于Python工具,如何使用Web3.py进行以太坊智能合约开发
  • 【简信CRM-注册安全分析报告】
  • ssm+vue694基于Java的药店药品信息管理系统的设计与实现
  • Sentinel微服务保护
  • 喜讯!实在Agent智能体入选《2024年度最佳企业服务AI产品榜》
  • Aop+自定义注解实现数据字典映射
  • 大语言模型(LLM)入门级选手初学教程 III
  • STM32G0xx使用LL库将Flash页分块方式存储数据实现一次擦除可多次写入
  • SAP B1 认证考试习题 - 解析版(三)
  • 数据库开发规范
  • 使用python向钉钉群聊发送消息
  • YOLOv11改进:SE注意力机制【注意力系列篇】(附详细的修改步骤,以及代码,与其他一些注意力机制相比,不仅准确度更高,而且模型更加轻量化。)
  • STM32 基于HAL库和STM32cubeIDE的应用教程 (二)--GPIO的使用
  • 【毫米波雷达(七)】自动驾驶汽车中的精准定位——RTK定位技术
  • Transformer和BERT的区别
  • linux 加载uPD720201固件
  • C语言中的信号量semaphore详解
  • 0087__DirectX11 With Windows SDK--02 顶点/像素着色器的创建、顶点缓冲区
  • Windows换机华为擎云(银河麒麟V10+麒麟9000C CPU)后,使用selenium的程序怎么办(20241030)
  • linux 下 signal() 函数的用法,信号类型在哪里定义的?
  • 享元模式及其运用场景:结合工厂模式和单例模式优化内存使用
  • 【物联网技术】ESP8266 WIFI模块在STA模式下实现UDP与电脑/手机网络助手通信——UDP数据透传
  • 【SQL Server】华中农业大学空间数据库实验报告 实验一 数据库
  • 操作系统页面置换算法Java实现(LFU,OPT,LRU,LFU,CLOCK)
  • Request和Response
  • 【青牛科技】GC8549替代LV8549/ONSEMI在摇头机、舞台灯、打印机和白色家电等产品上的应用分析
  • (十二)JavaWeb后端开发——MySQL数据库
  • pnpm管理多工作区依赖
  • 如何在本地Linux服务器搭建WordPress网站结合内网穿透随时随地可访问
  • 二、应用层,《计算机网络(自顶向下方法 第7版,James F.Kurose,Keith W.Ross)》