【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.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 组件中 借阅 按钮观察数据变化。

相关推荐
学习ing小白3 小时前
JavaWeb - 5 - 前端工程化
前端·elementui·vue
一只小阿乐3 小时前
前端web端项目运行的时候没有ip访问地址
vue.js·vue·vue3·web端
流氓也是种气质 _Cookie4 小时前
13 vue3之内置组件keep-alive
vue3·keep-alive
hello_fracong6 小时前
3.Vue2结合element-ui实现国际化多语言i18n
vue
码力码力我爱你11 小时前
QT + WebAssembly + Vue环境搭建
vue.js·vue·wasm·webassembly·emscripten
尸僵打怪兽19 小时前
后台数据管理系统 - 项目架构设计-Vue3+axios+Element-plus(0916)
vue.js·vue
是梦终空1 天前
JAVA毕业设计176—基于Java+Springboot+vue3的交通旅游订票管理系统(源代码+数据库)
java·spring boot·vue·毕业设计·课程设计·源代码·交通订票
天下无贼!1 天前
2024年最新版Vue3学习笔记
前端·vue.js·笔记·学习·vue
这个需求建议不做2 天前
vue3打包配置 vite、router、nginx配置
前端·nginx·vue
QGC二次开发2 天前
Vue3 : Pinia的性质与作用
前端·javascript·vue.js·typescript·前端框架·vue