首页 > 其他分享 >前端【TS】03-typescript【基础】【Pinia】

前端【TS】03-typescript【基础】【Pinia】

时间:2024-04-21 16:33:23浏览次数:21  
标签:count 03 typescript const TS Pinia import counterStore store

介绍

 

什么是Pinia

  Pinia 是 Vue 的专属的最新状态管理库 ,是 Vuex 状态管理工具的替代品

  

 

手动添加Pinia到Vue项目

  1. 使用 Vite 创建一个空的TS + Vue3项目

1 npm create vite@latest vue-pinia-ts -- --template vue-ts

  2. 按照官方文档安装 pinia 到项目中

 

Pinia基础使用 - 计数器案例

1、定义store

 1 // 创建计数器store
 2 
 3 // 1、导入defineStore
 4 import { defineStore } from 'pinia'
 5 import { computed, ref } from 'vue'
 6 
 7 // 2、通过defineStore创建store
 8 // 参数1为名称
 9 // 参数2为回调函数,返回一个对象,将state和方法返回
10 export const useCounterStore = defineStore('counter', () => {
11   // 数据state
12   const count = ref(0)
13 
14   // 修改数据的方法(action)
15   const increment = () => {
16     count.value++
17   }
18 
19   // getter
20   const doubleCount = computed(() => count.value * 2)
21 
22   // 以对象的方式return出去供组件使用
23   return {
24     count,
25     doubleCount,
26     increment,
27   }
28 })

2. 组件使用store

 1 <script setup lang="ts">
 2 // 使用counterStore
 3 
 4 // 1. 导入一个use打头的方法
 5 import { useCounterStore } from './store/counterStore'
 6 // 2. 执行方法得到store实例对象
 7 const counterStore = useCounterStore()
 8 </script>
 9 
10 <template>
11   <button @click="counterStore.increment">{{ counterStore.count }}</button>
12   getter:{{ counterStore.doubleCount }}
13 </template>

getters实现

  Pinia中的 getters 直接使用 computed函数 进行模拟, 组件中需要使用需要把 getters return出去

  

action异步实现

  方式:异步action函数的写法和组件中获取异步数据的写法完全一致

  

 

需求:在Pinia中获取频道列表数据并把数据渲染App组件的模板中

channelStore.ts

 1 // 1. 定义store结构
 2 
 3 import { defineStore } from 'pinia'
 4 import { ref } from 'vue'
 5 import axios from 'axios'
 6 
 7 // 2. 实现需求
 8 
 9 export const useChannelStore = defineStore('channel', () => {
10   // 1. 定义类型
11   type ChannelItem = {
12     id: number
13     name: string
14   }
15   type ResData = {
16     data: {
17       channels: ChannelItem[]
18     }
19     message: string
20   }
21 
22   // 2. 定义响应式数据 (state)
23   const list = ref<ChannelItem[]>([])
24 
25   // 3. axios获取数据 (异步action)
26   const getList = async () => {
27     const res = await axios.request<ResData>({
28       url: 'http://geek.itheima.net/v1_0/channels',
29     })
30     list.value = res.data.data.channels
31   }
32 
33   // 重要
34   return {
35     list,
36     getList,
37   }
38 })

App.vue  使用store

 1 <script setup lang="ts">
 2 // 1. 导入一个use打头的方法
 3 import { useChannelStore } from './store/channelStore'
 4 import { onMounted } from 'vue'
 5 import { storeToRefs } from 'pinia'
 6 // 2. 执行方法得到store实例对象
 7 const channelStore = useChannelStore()
 8 console.log(channelStore)
 9 
10 // 调用channelStore的异步action
11 onMounted(() => channelStore.getList())
12 
13 // 直接解构赋值(响应式丢失)
14 // const { count, doubleCount } = counterStore
15 
16 // 使用storeToRefs解构(只能支持数据: state/getters)
17 const { count, doubleCount } = storeToRefs(counterStore)
18 </script>
19 <template>
20   <ul>
21     <li v-for="item in channelStore.list" :key="item.id">{{ item.name }}</li>
22   </ul>
23 </template>

 

storeToRefs工具函数

  使用storeToRefs函数可以辅助保持数据(state + getter)的响应式解构

  

 

标签:count,03,typescript,const,TS,Pinia,import,counterStore,store
From: https://www.cnblogs.com/wang1001/p/18149106

相关文章

  • 前端【TS】02-typescript【基础】【搭建Vite+Vue3+TS项目】【为ref标注类型】
    前置基于Vite创建Vue3+TS环境vite官方文档:https://cn.vitejs.dev/guide/vite除了支持基础阶段的纯TS环境之外,还支持Vue+TS开发环境的快速创建,命令如下:1npmcreatevite@latestvue-ts-project----templatevue-ts23//说明:41.npmcreatevite@lates......
  • 站立会议和燃尽图03
    站立会议和燃尽图03一、小组情况组长:李宏威组员:董泽豪队名:隐约类名二、Scrum例会时间:2024年4月21日出席人员:李宏威,董泽豪要求1工作照片要求2时间跨度2024年4月16日7:00至2024年4月16日7:20共计20分钟要求3地点石家庄铁道大学要求4立会内容包括:(1)未开始......
  • 站立会议和燃尽图03
    站立会议和燃尽图03一、小组情况组长:李宏威组员:董泽豪队名:隐约类名二、Scrum例会时间:2024年4月21日出席人员:李宏威,董泽豪要求1工作照片要求2时间跨度2024年4月16日7:00至2024年4月16日7:20共计20分钟要求3地点石家庄铁道大学要求4立会内容包括:(1)未开始......
  • NanoPi-NEO 全志H3移植Ubuntu 22.04 LTS、u-boot、Linux内核/内核树、mt7601u USB-Wi-
    前言想在NanoPi-NEO上开发屏幕驱动,但是看了下文件目录发现没有内核树,导致最基础的file_operations结构体都无法使用,于是寻找内核树安装方法。但官方提供的内核为4.14太旧了apt找不到对应的linux-source版本(其实后面发现不需要用apt,可以在kernel.org上下载,但反正都装了那就当学习......
  • ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib
    ERROR2002(HY000):Can'tconnecttolocalMySQLserverthroughsocket'/var/lib/mysql/mysql.sock'(2)=====================================================步骤:以下可用。(1)关闭mysql:servicemysqldstop(2)查看mysql.sock的位置(base)[root@VM-0-2-ce......
  • .Net与AI的强强联合:AntSK知识库项目中Rerank模型的技术突破与实战应用
        随着人工智能技术的飞速发展,.Net技术与AI的结合已经成为了一个新的技术热点。今天,我要和大家分享一个令人兴奋的开源项目——AntSK,这是一个基于.net平台构建的开源离线AI知识库项目。在这个项目中,我们最近加入了一项强大的Rerank(重排)模型,进一步增强了我们的AI知识库的......
  • vue中ts引入组件,无法找到模块xxx的声明文件。xxx隐式拥有 "any" 类型。
    原因说明简单来说就是ts不认识.vue这个类型,需要定义声明。我刚学ts不是很懂为什么vite官方内写了那么多类型声明就是不写.vue。解决方法在项目根目录下找到env.d.ts文件,这个文件定义类型声明,简单地说就是让ts认识各种类型,尤其是文件。那么解决方法显而易见,我们自定义vue的......
  • 题解:P10365 [PA2024] Kraniki(评分:8.4)
    前言我们一场模拟赛的题,结果原题是新鲜出炉的。小弟不才,感觉这题是做过的题中几乎最复杂的了。既然搞懂了,就来写一发题解吧。(题外话:目前最优解,我的常数真是小小又大大啊)"Upanddown,glowin'round..."Solution1、一个经典的Trick直接模拟每一种情况显然不可取,考虑计算每......
  • ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(1)
    ASP.NETMVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(1) 本系列的的角色权限管理主要采用Dotnet MVC4工程内置的权限管理模块Simplemembership实现,主要有关文件是InitializeSimpleMembershipAttribute.cs和AccountModels.cs下面是对这两个文件的了解和改造 WebSe......
  • Computer Basics 03 - Basic Parts of a Computer
     IntroductionThebasicpartsofadesktopcomputerarethecomputercase,monitor,keyboard,mouse,andpowercord.Eachpartplaysanimportantrolewheneveryouuseacomputer.Watchthevideobelowtolearnaboutthebasicpartsofacomputer.(Video......