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

Pinia的基本用法

Pinia的安装和引入

1.安装Pinia

npm install pinia

2. 在vue项目的main.js文件中引入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')

Pinia的基本使用

使用pinia实现计数器

1.在src目录下创建stores目录,并新建文件counter.js
在这里插入图片描述
**2. 在counter.js文件中使用defineStore定义对象useCounterStore **

import { defineStore } from 'pinia'
import { ref } from 'vue'export const useCounterStore = defineStore('counter', () => {// 定义数据(state)const count = ref(0)// 定义修改数据的方法(action 同步+异步)const increment = () => {count.value++}// 以对象的方式return供组件使用return {count,increment}
})

**3.在App.vue文件中导入counter.js文件中的useCounterStore **

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

getters和异步action

在counter.js文件中进行如下定义

import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import axios from 'axios'const API_URL = "http://geek.itheima.net/v1_0/channels"
export const useCounterStore = defineStore('counter', () => {// 定义数据(state)const count = ref(0)// 定义修改数据的方法(action 同步+异步)const increment = () => {count.value++}// getter定义const doubleCount = computed(() => count.value * 2)// 定义异步actionconst list = ref([])const getList = async () => {const res = await axios.get(API_URL)list.value = res.data.data.channels}// 以对象的方式return供组件使用return {count,doubleCount,increment,list,getList}
})

在App.vue中使用

<script setup>
// 1. 导入use打头的方法
import { useCounterStore } from '@/stores/counter'
import { onMounted } from 'vue';
// 2. 执行方法得到store实例对象
const counterStore = useCounterStore()
console.log(counterStore)
onMounted(()=>{ // 挂载期发起请求counterStore.getList()
})
</script><template><button @click="counterStore.increment"> {{ counterStore.count}} </button>{{ counterStore.doubleCount }}<ul><li v-for="item in counterStore.list" :key="item.id">{{ item.name }}</li></ul>
</template><style scoped></style>

使用storeToRefs进行结构赋值,保持响应式更新

<script setup>
// 1. 导入use打头的方法
import { useCounterStore } from '@/stores/counter'
import { onMounted } from 'vue';
import { storeToRefs } from 'pinia'
// 2. 执行方法得到store实例对象
const counterStore = useCounterStore()
console.log(counterStore)// 直接结构赋值(响应式丢失)
// const { count, doubleCount} = counterStore
// console.log(count, doubleCount)// 方法包裹(保持响应式更新)
const { count, doubleCount } = storeToRefs(counterStore)
console.log(count, doubleCount)// 方法直接从原来的counterStore解构赋值
const { increment } = counterStore// 触发action
onMounted(()=>{ counterStore.getList()
})
</script><template><button @click="increment"> {{ count }} </button>{{ doubleCount }}<ul><li v-for="item in counterStore.list" :key="item.id">{{ item.name }}</li></ul>
</template><style scoped></style>
http://www.lryc.cn/news/384434.html

相关文章:

  • 正版软件 | DeskScapes:将您的桌面变成生动的画布
  • OpenCV cv::Mat到 Eigen 的正确转换——cv2eigen
  • PostgreSQL的扩展(extensions)-常用的扩展-pg_pathman
  • 数据结构之树
  • 6毛钱SOT-23封装28V、400mA 开关升压转换器,LCD偏置电源和白光LED应用芯片TPS61040
  • saga模型
  • 深度神经网络:解锁智能的密钥
  • 国际现货黄金最新价格如何分析?结合较高的时间周期
  • 微服务和kafka
  • Jetpack架构组件_Navigaiton组件_1.Navigaiton切换Fragment
  • [计算机网络] 虚拟局域网
  • LabVIEW遇到无法控制国外设备时怎么办
  • .hmallox勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • Redis发布、订阅模式(Pub/Sub)详解
  • Django-开发一个列表页面
  • flink 处理函数和流转换
  • 详细分析Springmvc中的@ModelAttribute基本知识(附Demo)
  • 和利时SIS安全系统模块SGM210 SGM210-A02
  • 浔川3样AI产品即将上线!——浔川总社部
  • 小阿轩yx-MySQL索引、事务
  • 搞定求职难题:工作岗位列表+简历制作工具 | 开源专题 No.75
  • JavaWeb——MySQL数据库:约束
  • JS(JavaScript)入门指南(DOM、事件处理、BOM、数据校验)
  • 江协科技51单片机学习- p16 矩阵键盘
  • grpc学习golang版( 四、多服务示例)
  • Linux安装jdk17
  • Java家教系统小程序APP公众号h5源码
  • PHP入门
  • docker ce的使用介绍
  • SpringCloud Alibaba Sentinel 流量控制之流控模式实践总结