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

js获取明天日期、Vue3大菠萝 Pinia的使用

直接上代码

const today = new Date(2019, 2, 28)
const finalDate = new Date(today)
finalDate.setDate(today.getDate() + 3)console.log(finalDate) // 31 March 2019
  • 安装

yarn add pinia
# or with npm
npm install pinia
  • 创建第一个store仓库

1、在src目录下创建store目录

2、创建一个 PiniaTest.ts 的文件(文件名可以根据自己需求来)

import {defineStore} from 'pinia'// 使用 defineStore 定义一个仓库,
// 组件中需要引入仓库,并使用useStorePinia 进行实例化 
// main 是仓库唯一的ID,可以根据自己的需求定义
export const useStorePinia = defineStore('main', {// 闭包state () {return {msg: 'hello word',count: 10,content: '这是通过getters获取Pinia管理的仓库数据'}},// 简写方式// state: () => ({//    msg: 'hello word',//    count: 10// }),getters:{getMsgFn(){return this.content}},actions:{// actions 里面可以执行同步和异步任务// 也可以接收外部传递进来的参数// 可以直接修改仓库的值,此处不能使用箭头函数,否则找不到this,打印显示 undefinedchangeMsg (val) {console.log('传入进来的值:', val)this.msg = '1111'this.count == val;}}
})
  • 引入并使用步骤

  1. 在 main.js 里面引入pinia

  1. 使用 createPinia 进行实例化

  1. 挂载到Vue身上(实际上是将插件进行注册,给放到已经注册的插件数组列表中)

import { createApp } from 'vue'
// 引入inia
import {createPinia} from 'pinia'
import App from './App.vue'console.log('createPinia:', createPinia);// 创建实例
const pinia = createPinia();console.log('pinia:', pinia);// 使用插件
createApp(App).use(pinia).mount('#app')
  • Pinia在组价中的使用

<template><div style="background: pink;padding: 10px;margin: 10px 0;"><div>组件11111111111</div><div>仓库数据:{{count}}---{{getMsgFn}}</div><button @click="changeStoreCountFn">点击</button></div>
</template><script setup>
import {defineProps} from 'vue';
import { storeToRefs } from "pinia";// 引入仓库
import {useStorePinia} from '../Store/PiniaTest'// 此处 defineStore 与仓库名一样
const store = useStorePinia();// 此处可以获取getters 和 actions 中的函数,但是不能直接获取仓库中的数据,需要使用storeToRefs强转ref类型
const {changeMsg, getMsgFn} = store;// 只有强转ref后数据才是响应式的
const {msg, count} = storeToRefs(store);
console.log(11111, store,storeToRefs(store), msg, count)// 修改仓库count值
const changeStoreCountFn = () => {// 方式 1、通过触发仓库 actions 中定义的函数执行changeMsg(++count.value)console.log(2222,getMsgFn)// 方式 2、读取仓库数据进行修改// count.value++// msg.value = 'aaaaaa'// 方式 3、对象形式修改仓库数据// store.$patch({//     msg: 'change word',//     count: ++count.value// })// 方式 4、函数形式修改仓库数据// store.$patch((state) =>{//     state.msg = 'change word';//     state.count++// })
}
</script>
http://www.xdnf.cn/news/3949.html

相关文章:

  • 【Linux系统篇】:Linux线程互斥---如何用互斥锁守护多线程程序
  • MCUboot 中的 BOOT_SWAP_TYPE_PERM 功能介绍
  • (undone) MIT6.S081 2023 学习笔记 (Day11: LAB10 mmap)
  • Redis数据结构ZipList,QuickList,SkipList
  • 《数字图像处理(面向新工科的电工电子信息基础课程系列教材)》封面颜色空间一图的选图历程
  • 电磁气动 V 型球阀:颗粒状矿浆与煤黑水介质处理的革命性解决方案-耀圣
  • GAF-CNN-SSA-LSSVM故障诊断/分类预测,附带模型研究报告(Matlab)
  • 学习海康VisionMaster之亮度测量
  • 图像批量处理工具 界面直观易懂
  • TCP 与 UDP报文
  • Doo全自动手机壳定制系统
  • 【AI大模型学习路线】第一阶段之大模型开发基础——第四章(提示工程技术-1)Zero-shot与Few-shot。
  • 基于 jQuery 实现灵活可配置的输入框验证功能
  • 模型 - Xiaomi MiMo
  • Sui 上线两周年,掀起增长「海啸」
  • 【PostgreSQL数据分析实战:从数据清洗到可视化全流程】5.3 相关性分析(PEARSON/SPEARMAN相关系数)
  • MongoDB入门详解
  • 永磁同步电机控制算法--基于PI和前馈的位置伺服控制
  • C 语言 第五章 指针(7)
  • LLM提示词设计及多轮对话优化策略在心理健康咨询场景中的应用研究
  • 从零开始学习RAG
  • Jetpack Compose 响应式布局实战:BoxWithConstraints 完全指南
  • 从0到1快速了解Redis数据库
  • 数字化转型:激活存量,引爆增量的三大核心逻辑
  • Spring-使用Java的方式配置Spring
  • 基于Python+MongoDB猫眼电影 Top100 数据爬取与存储
  • 常用CPU、GPU、NPU、DSP、ASIC等芯片区别介绍
  • RGB三原色
  • MATLAB仿真定点数转浮点数(对比VIVADO定点转浮点)
  • 【AI论文】像素修补师(PixelHacker):具有结构和语义一致性的图像修复(Image Inpainting)