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

【piania 的用法】

piania 的用法

  • 定义store
  • 建议使用箭头函数
  • TypeScript
  • 插件扩展
    • 1、全局添加对象

定义store

import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
// pinia 以函数的形式暴露出去
export const useCounterStore = defineStore('counter', () => {// 1、ref 相当于 stateconst count = ref(1)// 2、computed 相当于 getterconst doubleCount = computed(() => count.value * 2)// 3、function 相当于 actionfunction increment():void {count.value++}return { count, doubleCount, increment }
})

使用

import { useCounterStore } from '@/stores/counter'const counter = useCounterStore()

建议使用箭头函数

import { defineStore } from 'pinia'const useStore = defineStore('storeId', {// 为了完整类型推理,推荐使用箭头函数state: () => {return {// 所有这些属性都将自动推断出它们的类型count: 0,name: 'Eduardo',isAdmin: true,items: [],hasChanged: true,}},
})

TypeScript

// 定义接口
interface State {userList: UserInfo[]user: UserInfo | null
}const useStore = defineStore('storeId', {state: (): State => {return {userList: [],user: null,}},
})interface UserInfo {name: stringage: number
}

插件扩展

1、全局添加对象

import { createPinia } from 'pinia'// 创建的每个 store 中都会添加一个名为 `secret` 的属性。
// 在安装此插件后,插件可以保存在不同的文件中
function SecretPiniaPlugin() {return { secret: 'the cake is a lie' }
}const pinia = createPinia()
// 将该插件交给 Pinia
pinia.use(SecretPiniaPlugin)// 在另一个文件中
const store = useStore()
store.secret // 'the cake is a lie'

这对添加全局对象很有用,如路由器、modal 或 toast 管理器。

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

相关文章:

  • 上海计算机考研炸了,这所学校慎报!上海大学计算机考研考情分析!
  • 面对全球新能源汽车合作发展创维汽车如何实现共赢
  • 安全和加密常识(1)对称加密和非对称加密以及相应算法
  • afrog-漏洞扫描(挖洞)工具【了解安装使用详细】
  • c++类模板--无法解析的外部符号
  • Postman介绍
  • 以智能化为舵手,引领现代计算机系统架构新航向
  • 揭秘品牌成功秘诀:品牌营销策略的核心要素大公开
  • java如何把list转换成map
  • vite typescript 配置跨域代理
  • ArcGIS Pro SDK (七)编辑 10 捕捉
  • 开始尝试从0写一个项目--后端(一)
  • STM32第十二课:ADC检测烟雾浓度(MQ2)
  • 1975react社区问答管理系统开发mysql数据库web结构node.js编程计算机网页源码
  • SSL/CA 证书及其相关证书文件解析
  • 鸿蒙小案例-自定义键盘
  • STM32智能农业监控系统教程
  • 分子AI预测赛笔记
  • 003 线程的暂停和中断
  • mysql在部署时的问题
  • Flutter集成高德导航SDK(Android篇)(JAVA语法)
  • 代码随想录Day76(图论Part11)
  • 工程化:Commitlint / 规范化Git提交消息格式
  • 电脑有线网卡和无线网卡的MAC地址
  • 代码随想录-DAY②-数组——leetcode 977 | 209
  • 稀疏数组搜索
  • 存储器类型介绍
  • 论文学习笔记1:Federated Graph Neural Networks: Overview, Techniques, and Challenges
  • [数据集][目标检测]轮椅检测数据集VOC+YOLO格式13826张1类别
  • 视频剪辑音乐自动卡点Pr插件 BeatEdit v2.2 免费下载