【Vue3】Pinia store 组合式写法

【Vue3】Pinia store 组合式写法

背景

随着年龄的增长,很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来,技术出身的人总是很难放下一些执念,遂将这些知识整理成文,以纪念曾经努力学习奋斗的日子。本文内容并非完全原创,大多是参考其他文章资料整理所得,感谢每位技术人的开源精神。

简介

本文介绍 Vue3 中如何编写 Pinia store 的组合式写法。

Pinia 是 Vue 专属的状态管理库,允许跨组件或页面共享数据。

开发环境

分类名称版本
操作系统WindowsWindows 11
IDEVisual Studio Code1.91.1

开发步骤及源码

1> 在 【Vue3】Pinia存储及读取数据 基础上修改 src/store/book.ts,将选项式写法改成组合式写法。

import { defineStore } from "pinia"

export interface Book {
    id: string
    title: string
    author: string
    category: string
}

import { ref, reactive, computed } from 'vue'

export const useBookStore = defineStore('book', () => {
    // 对应选项式写法中 state() 代码
    const bookCount = ref(6)
    const books = reactive([
        { id: '001', title: '坐天下', author: '张宏杰', category: "历史" },
        { id: '002', title: '明朝那些事儿', author: '当年明月', category: "历史" },
        { id: '003', title: '太白金星有点烦', author: '马伯庸', category: "小说" },
        { id: '004', title: '活着', author: '余华', category: "小说" },
        { id: '005', title: '饥饿的盛世', author: '张宏杰', category: "历史" },
        { id: '006', title: '镖人', author: '许先哲', category: "漫画" },
    ])

    // 对应选项式写法中 actions 代码
    function borrow(): Book | undefined {
        if (bookCount.value > 0) {
            bookCount.value -= 1
        }
        return books.shift()
    }

    // 对应选项式写法中 getters 代码
    const history = computed(() => books.filter((book: Book) => book.category == "历史").length)
    const novel = computed(() => books.filter((book: Book) => book.category == "小说").length)
    const comic = computed(() => books.filter((book: Book) => book.category == "漫画").length)

    // 注意一定要通过 return 对外暴露,否则外部无法访问
    return { bookCount, books, borrow, history, novel, comic }
})

注意:

  • 将选项式写法中 state() 定义的数据改为普通变量(或常量)定义;
  • 将选项式写法中 actions 中定义的行为方法替换成普通方法定义;
  • 将选项式写法中 getters 中定义的计算属性替换成普通计算属性(computed)定义;
  • 使用 return 返回定义的数据、行为方法及计算属性,这样外部才能使用。

2> 修改功能组件 src/components/Book.vue,调用 book.ts 中定义的数据和计算属性进行展示。

<template>
    <div class="books">
        <h2>图书数量:{{ bookCount }}</h2>
        <h2>历史类:{{ bookStore.history }}</h2>
        <h2>小说类:{{ bookStore.novel }}</h2>
        <h2>漫画类:{{ bookStore.comic }}</h2>
        <h2>图书列表</h2>
        <ul>
            <li v-for="book in books" :key="book.id">
                {{ book.title }} -- {{ book.author }}
            </li>
        </ul>
    </div>
</template>

<script setup lang="ts">
import { useBookStore } from '@/store/book'
import { storeToRefs } from 'pinia';

const bookStore = useBookStore()
const { bookCount, books } = storeToRefs(bookStore)
</script>

<style scoped lang="scss">
.books {
    background-color: aquamarine;
    padding: 20px;
    li {
        font-size: 20px;
        height: 35px;
        line-height: 35px;
    }
}
</style>

3> 定义功能组件 Reader.vue,调用 book.ts 中定义的行为方法修改 Pinia 中数据。

<template>
    <div class="reader">
        <button @click="borrow">借阅</button>
        <hr>
        <h3>借阅列表</h3>
        <ol>
            <li v-for="book in books" :key="book.id">
                {{ book.title }} -- {{ book.author }}
            </li>
        </ol>
    </div>
</template>

<script setup lang="ts">
import { useBookStore, type Book } from '@/store/book'
import { reactive } from 'vue'

const bookStore = useBookStore()

const books = reactive<any>([])

function borrow() {
    let book = bookStore.borrow()
    if (book) {
        books.push(book)
    }
}
</script>

<style scoped lang="scss">
.reader {
    background-color: darkcyan;
    padding: 20px;
    button {
        font-size: 20px;
        height: 40px;
        line-height: 40px;
        margin-right: 10px;
        width: 120px;
    }
    li {
        color: white;
        font-size: 20px;
        height: 35px;
        line-height: 35px;
    }
}
</style>

4> 修改根组件 App.vue,引用 Book.vueReader.vue

<template>
  <div class="content">
    <Book />
    <hr>
    <Reader />
  </div>
</template>

<script setup lang="ts">
import Book from './components/Book.vue'
import Reader from './components/Reader.vue'
</script>

<style scoped lang="scss">
.content {
  background-color: darkgray;
  padding: 20px;
}
</style>

5> 执行命令 npm run dev 启动应用,浏览器访问:http://localhost:5173/,点击 Reader.vue 组件中 借阅 按钮观察数据变化。
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue 3 是一个非常强大的 JavaScript 框架,而 Pinia 是一个简单而强大的状态管理库。在 Vue 3 中,你可以使用 Pinia 来管理全局状态和局部状态。下面是一个简单的介绍Vue 3中Pinia的写法。 首先,在你的项目中安装 Pinia 依赖: ``` npm install pinia ``` 接着,创建一个 store.js 文件,并编写你的 Pinia Store。例如: ``` import { createPinia } from 'pinia' const pinia = createPinia() export const useCounterStore = pinia.createStore({ state: () => ({ count: 0 }), actions: { increment() { this.count++ } } }) ``` 在上面的代码中,我们首先引入了 Pinia 的 createPinia 方法,并使用它创建了一个新的 pinia 实例。然后我们使用 `createStore` 方法创建了一个名为 `useCounterStore` 的 store,其中包含一个 `count` 状态和一个 `increment` action,用于增加 `count` 的值。 在你的组件中使用这个 store,你需要先引入它: ``` import { useCounterStore } from './store.js' ``` 然后在组件中使用这个 store: ``` <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> </div> </template> <script> import { useCounterStore } from './store.js' export default { setup() { const counterStore = useCounterStore() return { count: counterStore.count, increment: counterStore.increment } } } </script> ``` 在上面的代码中,我们通过 `useCounterStore()` 方法获取了 `useCounterStore` store 的实例,并将其返回的 `count` 和 `increment` 绑定到组件中。 至此,你已经完成了使用 Pinia 管理状态的全部过程。当然,还有很多高级用法需要深入学习。下面是相关问题:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

又言又语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值