当前位置: 首页 > news >正文

vuex和pinia区别

Pinia 相比 Vuex,在 TypeScript 方面提供了更好的支持,主要体现在以下几个方面:

  1. 类型推导更友好

Pinia 采用的是函数式 API,直接基于 defineStore 创建 store,类型可以被 TypeScript 自动推导,无需手动定义复杂的类型。

Vuex 的类型定义(繁琐)

在 Vuex 中,需要手动定义 State 类型,并通过泛型约束 Getter、Mutation、Action:

// 定义 State 类型
interface State {
count: number;
}

// Vuex Store
const store = new Vuex.Store({
state: {
count: 0,
},
mutations: {
increment(state) {
state.count++;
},
},
actions: {
asyncIncrement({ commit }) {
commit(“increment”);
},
},
getters: {
doubleCount: (state) => state.count * 2,
},
});

使用时还要手动定义 mapState,并且 dispatch 也需要额外封装类型。


Pinia 的类型推导(更简洁)

Pinia 直接支持 TypeScript 类型推导,不需要手动声明 State 类型:

import { defineStore } from “pinia”;

export const useCounterStore = defineStore(“counter”, {
state: () => ({
count: 0,
}),
actions: {
increment() {
this.count++;
},
},
getters: {
doubleCount: (state) => state.count * 2,
},
});

在组件中使用时,useCounterStore() 会自动推导出 count 的类型:

const store = useCounterStore();
store.count; // ✅ 自动推导为 number
store.increment(); // ✅ 自动识别方法类型

  1. 更好的 getter 类型支持

Pinia 的 getters 可以直接推导 state 的类型,而 Vuex 需要手动声明:

export const useCounterStore = defineStore(“counter”, {
state: () => ({
count: 0,
}),
getters: {
doubleCount: (state) => state.count * 2, // ✅ 自动推导 doubleCount 为 number
},
});

而 Vuex 需要手动为 getters 额外声明类型:

interface Getters {
doubleCount: number;
}

  1. storeToRefs 解构后仍然保留响应式

在 Vuex 中,解构 state 可能会失去响应式,而 Pinia 提供 storeToRefs(),解构后仍然能保持响应式:

import { storeToRefs } from “pinia”;

const store = useCounterStore();
const { count } = storeToRefs(store); // ✅ 仍然是响应式

Vuex 需要 computed(() => store.state.count) 来保持响应式,更加繁琐。

  1. 直接使用 this 访问 state 和 actions

Pinia 允许在 actions 里直接使用 this 访问 state 和其他 actions:

export const useCounterStore = defineStore(“counter”, {
state: () => ({
count: 0,
}),
actions: {
increment() {
this.count++; // ✅ this 直接访问 state
},
asyncIncrement() {
setTimeout(() => {
this.increment(); // ✅ this 直接调用 actions
}, 1000);
},
},
});

Vuex 需要 commit 和 dispatch,而 Pinia 直接使用 this,更加直观。
在这里插入图片描述
Pinia 通过更好的类型推导、自动识别 state 和 getter,减少了对手动声明类型的依赖,使 TypeScript 支持更加友好。

相关文章:

  • 16 网络服务应用
  • 软考中级-软件设计师 2023年上半年上午题真题解析:通关秘籍+避坑指南
  • 在 Ubuntu 22.04 上安装 Docker Compose 的步骤
  • 帕累托分布的均值和方差
  • 【第一节】Python爬虫基础-HTTP基本原理
  • 基于 Hough 变换的直线检测
  • 高性能计算面经
  • Docker学习之容器虚拟化与虚拟机的区别(day11)
  • python笔记:unsloth (Basic)
  • Python项目-基于Flask的个人博客系统设计与实现(2)
  • 【系统移植】(一)概念流程
  • 零基础玩转深度神经网络大模型:从Hello World到AI炼金术(详解版)
  • 如何在中科方德llinux系统上离线安装salt-minion
  • AIP-210 Unicode
  • 【初阶数据结构】队列
  • 浅谈多个虚拟机(WSL和VMWare)的网络配置
  • HarmonyOS 5.0.4(16) 版本正式发布,支持wearable类型的设备!
  • 【C++】vector常用方法总结
  • 深度学习入门:小白的奇妙之旅
  • 2025.4.1总结
  • wordpress 咚门下载/优化防控举措
  • 廊坊市网站建设/互联网营销师报名入口官网
  • 建设企业网站方法/seo排名软件有用吗
  • jsp的网站/郑州企业网站优化排名
  • 重庆玖玺国际做网站/百度链接收录提交入口
  • 广州市民政局网站建设/最新seo操作