【Vue3】Pinia store 组合式写法

【Vue3】Pinia store 组合式写法

背景

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

简介

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

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

开发环境

分类 名称 版本
操作系统 Windows Windows 11
IDE Visual Studio Code 1.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.vue 和 Reader.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 组件中 借阅 按钮观察数据变化。

相关推荐
Nymph_Zhu18 小时前
vue3+element-plus 文件上传
前端·typescript·vue3·element-plus
Nymph_Zhu19 小时前
pdf转换markdown预览及内容互相点击定位
前端·pdf·vue3·markdown·element-plus
汤米粥2 天前
2026年主流前端技术框架
vue·react·elementplus·tailwind css·angular‌·svelte‌·bootstrap‌
codigger2 天前
Vue nextTick 到底在等什么?一篇讲透原理与那些年踩过的坑
程序员·vue·编程·异步更新·#人工智能
咚咚咚小柒3 天前
【前端】LangChain.js学习
前端·javascript·人工智能·typescript·langchain·vue·ai编程
AI源码4 天前
巨峰影视影院
vue3
RuoyiOffice4 天前
UniApp企业办公:把审批、客户和项目装进手机,能做些什么?
spring boot·vue3·uniapp·crm·flowable·移动办公·企业应用
悟道书生5 天前
若依 RBAC 权限控制学习笔记
java·网络·笔记·sql·学习·vue
mewowaBc5 天前
使用Pinia持久化存储实现用户认证系统
vue·pinia·用户认证系统
RuoyiOffice5 天前
SpringBoot OA推荐:面向信创选型,看看这套企业协同平台
spring boot·vue3·信创·flowable·企业协同·ruoyi office·oa选型