Vue3 状态管理之 Pinia 的使用

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MuxYBwXS-1646496904674)(https://pinia.vuejs.org/logo.svg#pic_center)]Vue3 新的发展方向(来源于尤大知乎)Vue 3 将在 2022 年 2 月 7 日 成为新的默认版本基于 Vite 的极速构建工具链<script setup> 带来的开发体验更丝滑的组合式 API 语法Volar 提供的单文件组件 TypeScript
摘要由CSDN通过智能技术生成

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MuxYBwXS-1646496904674)(https://pinia.vuejs.org/logo.svg#pic_center)]

Vue3 新的发展方向(来源于尤大知乎)

  • Vue 3 将在 2022 年 2 月 7 日 成为新的默认版本

  • 基于 Vite 的极速构建工具链

  • <script setup> 带来的开发体验更丝滑的组合式 API 语法

  • Volar 提供的单文件组件 TypeScript IDE 支持

  • vue-tsc 提供的针对单文件组件的命令行类型检查和生成

  • Pinia 提供的更简洁的状态管理

  • 新的开发者工具扩展,同时支持 Vue 2/Vue 3,并且提供一个插件系统来允许社区库自行扩展开发者工具面板。

一、Pinia 简介与基础

1.1 Pinia 简介

  • 官方地址:[这里是代码003]
  • PiniaVuex4 的升级版,也就是 Vuex5
  • Pinia 极大的简化了Vuex的使用,是 Vue3的新的状态管理工具
  • Piniats的支持更好,性能更优, 体积更小,无 mutations,可用于 Vue2Vue3
  • Pinia支持Vue Devtools、 模块热更新和服务端渲染

1.2 Pinia 基础

Vuex 与 Pinia 对比

  • Vuex 中核心部分: StateGettersMutations(同步) 和 Actions(异步)
  • Pinia 中核心部分: StateGettersActions(同步异步均支持)

Pinia 各部分作用

  • State 类似于组件中data,用于存储全局状态
  • Getters 类似于组件中的computed,根据已有的State封装派生数据,也具有缓存的特性
  • Actions 类似于组件中的methods,用于封装业务逻辑,同步异步均可以

Pinia 官方示例JS版本

import { defineStore } from 'pinia'

export const todos = defineStore('todos', {
  state: () => ({
    /** @type {
  { text: string, id: number, isFinished: boolean }[]} */
    todos: [],
    /** @type {'all' | 'finished' | 'unfinished'} */
    filter: 'all',
    // type will be automatically inferred to number
    nextId: 0,
  }),
  getters: {
    finishedTodos(state) {
      // autocompletion! ?
      return state.todos.filter((todo) => todo.isFinished)
    },
    unfinishedTodos(state) {
      return state.todos.filter((todo) => !todo.isFinished)
    },
    /**
     * @returns {
  { text: string, id: number, isFinished: boolean }[]}
     */
    filteredTodos(state) {
      if (this.filter === 'finished') {
        // call other getters with autocompletion ?
        return this.finishedTodos
      } else if (this.filter === 'unfinished') {
        return this.unfinishedTodos
      }
      return this.todos
    },
  },
  actions: {
    // any amount of arguments, return a promise or not
    addTodo(text) {
      // you can directly mutate the stat 00e
      this.todos.push({ text, id: this.nextId++, isFinished: false })
    },
  },
})

二、Pinia 在Vue3-Vite中的使用

2.1 基础使用流程

  • ① 创建一个vue vite项目

    PS C:UsersFORGETDesktopvue-pinia-demo> npm init vite@latest
    Need to install the following packages:
    create-vite@latest
    Ok to proceed? (y) y
    √ Project name: … pinia-demo
    √ Select a framework: ? vue
    √ Select a variant: ? vue-ts

    Scaffolding project in C:UsersFORGETDesktopvue-pinia-demopinia-demo…

    Done. Now run:

    cd pinia-demo
    npm install
    npm run dev
    PS C:UsersFORGETDesktopvue-pinia-demo> cd .pinia-demo
    PS C:UsersFORGETDesktopvue-pinia-demopinia-demo> npm install

  • ② 安装 pinia-S是为了将其保存至package.json中,便于Git管理给其他人的使用

    PS C:UsersFORGETDesktopvue-pinia-demopinia-demo> npm install pinia -S

    package.json文件中

    “dependencies”: {
    “pinia”: “^2.0.9”,
    “vue”: “^3.2.25”
    },

  • ③ 创建 pinia 实例并挂载到 vue

    // main.ts 文件
    import { createApp } from ‘vue’
    import App from ‘./App.vue’
    import {createPinia} from ‘pinia’
    // 创建 Pinia 实例
    const pinia = createPinia()
    // 创建 Vue 实例
    const app

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值