Vue 全局状态管理:Vuex 从入门到精通

前言

在开发 Vue 应用时,管理组件之间的状态共享变得越来越重要。特别是当应用变得复杂,组件之间的通信越来越多时,如何有效地管理状态成为了一个重要问题。Vuex 是 Vue 提供的一种集中式状态管理模式,能够帮助开发者更好地管理应用状态,提高代码的可维护性和可扩展性。
本文将通过通俗易懂的方式介绍 Vuex 的基本用法以及一些进阶技巧,帮助你更好地掌握 Vuex。

Vuex 是什么?

Vuex 是一个专为 Vue.js 应用设计的状态管理模式。它借鉴了 Flux、Redux 等状态管理库的思想,通过一个全局的 store 来管理应用的所有状态,并且保持状态的唯一性和可预测性。

简单来说,Vuex 可以理解为一个专门用来管理应用状态的超大仓库,我们可以把应用中所有组件需要共享的状态集中放在这个仓库中。通过 Vuex,我们可以轻松地从仓库中获取状态,更新状态,并且这些状态的变化能被自动地同步到使用它们的组件中。

Vuex 的基本概念

在开始使用 Vuex 之前,我们需要了解以下几个基本概念:

  1. State:状态,存储应用的全局状态。
  2. Getter:计算属性,类似于组件中的计算属性,用于从 state 中派生一些状态。
  3. Mutation:突变,唯一可以修改 state 的方法,通过提交 mutation 来修改状态。
  4. Action:动作,和 mutation 类似,但它是用于处理异步操作的,可以包含任意异步逻辑。
  5. Module:模块,Vuex 支持将状态和变更逻辑按模块进行划分,方便管理。

Vuex 的基本使用

1. 安装 Vuex

在 Vue 项目中使用 Vuex 非常简单,首先需要进行安装:

npm install vuex --save

安装完成后,在项目中创建一个 store 文件夹,并创建一个 index.js 文件:

// src/store/index.js
import Vue from 'vue';
import Vuex from 'vuex';Vue.use(Vuex);const store = new Vuex.Store({state: {count: 0},mutations: {increment(state) {state.count++;}},actions: {incrementAsync({ commit }) {setTimeout(() => {commit('increment');}, 1000);}},getters: {doubleCount: state => state.count * 2}
});export default store;

2. 在 Vue 实例中使用 Vuex

接下来,我们需要在 Vue 实例中引入并使用这个 store:

// src/main.js
import Vue from 'vue';
import App from './App.vue';
import store from './store';new Vue({render: h => h(App),store
}).$mount('#app');

3. 在组件中使用 Vuex

在组件中,我们可以通过 this.$store 来访问 Vuex 的状态和方法。例如:

<template><div><p>{{ count }}</p><p>{{ doubleCount }}</p><button @click="increment">Increment</button><button @click="incrementAsync">Increment Async</button></div>
</template><script>
export default {computed: {count() {return this.$store.state.count;},doubleCount() {return this.$store.getters.doubleCount;}},methods: {increment() {this.$store.commit('increment');},incrementAsync() {this.$store.dispatch('incrementAsync');}}
};
</script>

进阶技巧

1. 模块化

当应用变得复杂时,将所有的状态和变更逻辑放在一个 store 中会显得很混乱。Vuex 支持将 store 拆分成模块,每个模块都拥有自己的 state、mutation、action 和 getter。

// src/store/modules/counter.js
const state = {count: 0
};const mutations = {increment(state) {state.count++;}
};const actions = {incrementAsync({ commit }) {setTimeout(() => {commit('increment');}, 1000);}
};const getters = {doubleCount: state => state.count * 2
};export default {state,mutations,actions,getters
};// src/store/index.js
import Vue from 'vue';
import Vuex from 'vuex';
import counter from './modules/counter';Vue.use(Vuex);const store = new Vuex.Store({modules: {counter}
});export default store;

2. 使用辅助函数

Vuex 提供了一些辅助函数,帮助我们更方便地在组件中使用 state、getter、mutation 和 action。例如 mapState、mapGetters、mapMutations 和 mapActions。

<template><div><p>{{ count }}</p><p>{{ doubleCount }}</p><button @click="increment">Increment</button><button @click="incrementAsync">Increment Async</button></div>
</template><script>
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex';export default {computed: {...mapState(['count']),...mapGetters(['doubleCount'])},methods: {...mapMutations(['increment']),...mapActions(['incrementAsync'])}
};
</script>

3. 插件与持久化

1. 使用 Vuex 插件

Vuex 支持使用插件来扩展其功能。插件可以用于日志记录、状态持久化、时间旅行等。插件是一些函数,它们会接收 store 作为参数。

例如:日志插件
我们可以创建一个简单的日志插件,记录每次 mutation 的调用情况:

// src/store/plugins/logger.js
const logger = store => {store.subscribe((mutation, state) => {console.log('Mutation:', mutation.type);console.log('Payload:', mutation.payload);});
};export default logger;

然后在创建 store 时,使用这个插件:

// src/store/index.js
import Vue from 'vue';
import Vuex from 'vuex';
import counter from './modules/counter';
import logger from './plugins/logger';Vue.use(Vuex);const store = new Vuex.Store({modules: {counter},plugins: [logger]
});export default store;
2. 状态持久化

在开发某些应用时,我们希望在页面刷新时保持 Vuex 的状态不变。我们可以使用 Vuex 插件 vuex-persistedstate 来实现状态持久化。

安装 vuex-persistedstate:

npm install vuex-persistedstate --save

使用 vuex-persistedstate 插件:

// src/store/index.js
import Vue from 'vue';
import Vuex from 'vuex';
import counter from './modules/counter';
import createPersistedState from 'vuex-persistedstate';Vue.use(Vuex);const store = new Vuex.Store({modules: {counter},plugins: [createPersistedState()]
});export default store;

通过这样简单的配置,Vuex 的状态就能够在页面刷新时持久化保存了。

4. 动态模块注册

在某些应用中,我们可能需要根据条件动态地注册 Vuex 模块。Vuex 提供了 registerModule 和 unregisterModule 方法来实现动态模块注册和注销。

动态注册模块
假设我们有一个用户模块,需要在用户登录后动态注册:

// src/store/modules/user.js
const state = {name: '',email: ''
};const mutations = {setUser(state, user) {state.name = user.name;state.email = user.email;}
};const actions = {login({ commit }, user) {// 模拟登录请求return new Promise(resolve => {setTimeout(() => {commit('setUser', user);resolve();}, 1000);});}
};export default {state,mutations,actions
};

在组件中,我们可以动态注册和使用该模块:

<template><div><button @click="login">Login</button></div>
</template><script>
import { mapActions } from 'vuex';export default {methods: {...mapActions(['login']),login() {const user = {name: 'John Doe',email: 'john@example.com'};this.$store.registerModule('user', require('@/store/modules/user').default);this.login(user).then(() => {console.log('User logged in');});}}
};
</script>

实践案例

构建一个简单的 Todo 应用

为了更好地理解和应用 Vuex,我们将通过构建一个简单的 Todo 应用来演示如何使用 Vuex 进行状态管理。

1. 项目结构
首先,让我们创建一个 Vue 项目,并安装 Vuex:

vue create vuex-todo-app
cd vuex-todo-app
npm install vuex --save

项目结构如下:

vuex-todo-app/
│
├── src/
│   ├── components/
│   │   └── TodoList.vue
│   ├── store/
│   │   └── index.js
│   ├── App.vue
│   └── main.js

2. 配置 Vuex Store
在 src/store/index.js 中配置我们的 Vuex store:

// src/store/index.js
import Vue from 'vue';
import Vuex from 'vuex';Vue.use(Vuex);const state = {todos: []
};const mutations = {ADD_TODO(state, todo) {state.todos.push(todo);},REMOVE_TODO(state, index) {state.todos.splice(index, 1);},TOGGLE_TODO(state, index) {state.todos[index].completed = !state.todos[index].completed;}
};const actions = {addTodo({ commit }, todo) {commit('ADD_TODO', todo);},removeTodo({ commit }, index) {commit('REMOVE_TODO', index);},toggleTodo({ commit }, index) {commit('TOGGLE_TODO', index);}
};const getters = {completedTodos: state => state.todos.filter(todo => todo.completed),pendingTodos: state => state.todos.filter(todo => !todo.completed)
};const store = new Vuex.Store({state,mutations,actions,getters
});export default store;

3. 主文件配置
在 src/main.js 中引入和配置 store:

// src/main.js
import Vue from 'vue';
import App from './App.vue';
import store from './store';Vue.config.productionTip = false;new Vue({render: h => h(App),store
}).$mount('#app');

4. 创建 TodoList 组件
在 src/components/TodoList.vue 中创建我们的 TodoList 组件:

<template><div><h1>Todo List</h1><input v-model="newTodo" @keyup.enter="addTodo" placeholder="Add a new todo" /><ul><li v-for="(todo, index) in todos" :key="index"><input type="checkbox" v-model="todo.completed" @change="toggleTodo(index)" /><span :class="{ completed: todo.completed }">{{ todo.text }}</span><button @click="removeTodo(index)">Remove</button></li></ul><h2>Completed Todos</h2><ul><li v-for="(todo, index) in completedTodos" :key="index">{{ todo.text }}</li></ul><h2>Pending Todos</h2><ul><li v-for="(todo, index) in pendingTodos" :key="index">{{ todo.text }}</li></ul></div>
</template><script>
import { mapState, mapGetters, mapActions } from 'vuex';export default {data() {return {newTodo: ''};},computed: {...mapState(['todos']),...mapGetters(['completedTodos', 'pendingTodos'])},methods: {...mapActions(['addTodo', 'removeTodo', 'toggleTodo']),addTodo() {if (this.newTodo.trim() !== '') {this.addTodo({text: this.newTodo,completed: false});this.newTodo = '';}}}
};
</script><style scoped>
.completed {text-decoration: line-through;
}
</style>

5. 使用 TodoList 组件
在 src/App.vue 中使用我们创建的 TodoList 组件:

<template><div id="app"><TodoList /></div>
</template><script>
import TodoList from './components/TodoList.vue';export default {name: 'App',components: {TodoList}
};
</script><style>
@import './assets/styles.css';
</style>

6. 运行应用
至此,我们的 Todo 应用已经完成。运行应用:

npm run serve

打开浏览器,访问 http://localhost:8080,你将看到一个简单的 Todo 应用,你可以添加、删除、标记完成和查看已完成或未完成的 Todo 项目。

总结

通过本文的介绍,我们了解了 Vuex 的基本概念和使用方法,并且学习了一些进阶技巧。掌握 Vuex 可以帮助我们更好地管理 Vue 应用中的状态,提高代码的可维护性和可扩展性。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/59512.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机视觉算法:从图像处理到智能识别

计算机视觉算法&#xff1a;从图像处理到智能识别 计算机视觉&#xff08;Computer Vision&#xff09;是人工智能领域的一个重要分支&#xff0c;旨在使计算机能够“看”和理解图像或视频中的内容。随着深度学习和大数据技术的快速发展&#xff0c;计算机视觉算法在图像识别、…

排序(用java实现)

排序 排序的概念及引用排序的概念常见的排序算法 常见排序算法的实现插入排序基本思想&#xff1a;直接插入排序希尔排序(缩小增量排序) 选择排序基本思想&#xff1a;直接选择排序堆排序 交换排序冒泡排序快速排序快速排序的优化快速排序非递归 归并排序基本思想归并排序归并排…

DNS查询服务器的全流程解析

### DNS查询服务器的基本流程&#xff0c;能画出图更好&#xff0c;并说明为什么DNS查询为什么不直接从单一服务器查询ip&#xff0c;而是要经过多次查询&#xff0c;多次查询不会增加开销么&#xff08;即DNS多级查询的优点&#xff09;&#xff1f; - **用户发起请求**&#…

(C++回溯算法)微信小程序“开局托儿所”游戏

问题描述 给定一个矩阵 A ( a i j ) m n \bm A(a_{ij})_{m\times n} A(aij​)mn​&#xff0c;其中 a i j ∈ { 1 , 2 , ⋯ , 9 } a_{ij}\in\{1,2,\cdots,9\} aij​∈{1,2,⋯,9}&#xff0c;且满足 ∑ i 1 m ∑ j 1 n a i j \sum\limits_{i1}^m\sum\limits_{j1}^na_{ij} i…

Go:接口和反射

接口 定义 在传统的面向对象的语言中&#xff0c;是会存在类和继承的概念的&#xff0c;但是Go并没有 那Go如何实现类似的方法呢&#xff1f;它提供了接口的概念&#xff0c;可以实现很多面向对象的特性 接口定义会实现一组方法集&#xff0c;但是这些方法不包含实现的代码…

QCustomPlot添加自定义的图例,实现隐藏、删除功能(一)

文章目录 实现步骤:实现代码:代码讲解:功能说明:优化建议:其他参考:要实现一个支持勾选并可以控制曲线显示和隐藏的自定义 QCPLegend 类,可以通过继承 QCPLegend 并重写其相关方法来实现。我们需要添加一个自定义的复选框元素,并捕捉用户交互来实现曲线的隐藏和显示。…

数字IC后端实现之Innovus Place跑完density爆涨案例分析

下图所示为咱们社区a7core后端训练营学员的floorplan。 数字IC后端实现 | Innovus各个阶段常用命令汇总 该学员跑placement前density是59.467%&#xff0c;但跑完place后density飙升到87.68%。 仔细查看place过程中的log就可以发现Density一路飙升&#xff01; 数字IC后端物…

聊一聊Elasticsearch的索引的分片分配机制

1、什么是分片分配 分片分配是由ES主节点将索引分片移动到ES集群中各个节点上的过程。 该过程尽量保证&#xff0c;同一个索引的分片尽量分配到更多的节点上&#xff0c;以此来达到读写索引的时候可以利用更多硬件资源的效果。 在分配过程当中&#xff0c;也不能将某个主分片…

最详细【Elasticsearch】Elasticsearch Java API + Spring Boot集成 实战入门(基础篇)

Elasticsearch Java API Spring Boot集成 实战入门&#xff08;基础篇&#xff09; 一、初始Elasticseach1、什么是Elasticseach2、Elasticsearch生态2、Elasticsearch结构3、Elasticsearch核心概念4、Elasticsearch 实现全文检索的原理 二、Elasticsearch入门1、入门-环境安装…

如何在 Spring Boot 中实现多数据源的事务管理?

在 Spring Boot 中实现多数据源的事务管理可以通过以下几种方式&#xff1a; 一、使用编程式事务管理 配置多个数据源 如同前面提到的&#xff0c;在 application.properties 或 application.yml 文件中配置多个数据源的连接信息&#xff0c;并创建对应的数据源 bean。 手动开启…

文件操作:Xml转Excel

1 添加依赖 Spire.Xls.jar <dependency><groupId>e-iceblue</groupId><artifactId>spire.xls</artifactId><version>5.3.3</version></dependency>2 代码使用 package cctd.controller;import com.spire.xls.FileFormat; im…

物理验证Calibre LVS Debug案例之通过deleteEmptyModule解决LVS问题

上周帮助T12nm A55训练营学员debug一个Calibre LVS问题&#xff0c;小编觉得挺好的一个问题。这个问题之前没有遇到过&#xff0c;今天分享给大家。 数字IC后端先进工艺设计实现之TSMC 12nm 6Track工艺数字IC后端实现重点难点盘点 下图所示为Calibre LVS的报告。从报告中看到…

深度解析阿里的Sentinel

1、前言 这是《Spring Cloud 进阶》专栏的第五篇文章&#xff0c;这篇文章介绍一下阿里开源的流量防卫兵Sentinel&#xff0c;一款非常优秀的开源项目&#xff0c;经过近10年的双十一的考验&#xff0c;非常成熟的一款产品。 文章目录如下&#xff1a; 2、什么是sentinel&…

ReactPress系列—Next.js 的动态路由使用介绍

ReactPress Github项目地址&#xff1a;https://github.com/fecommunity/reactpress 欢迎提出宝贵的建议&#xff0c;感谢Star。 Next.js 的动态路由使用介绍 Next.js 是一个流行的 React 框架&#xff0c;支持服务端渲染、静态站点生成和动态路由等功能&#xff0c;极大地简化…

arm架构部署nexus配置sslcontainerd

背景 合作伙伴私有云的机器架构是arm的&#xff0c;使用registry做为镜像仓库&#xff0c;可以满足基础功能&#xff0c;权限管理等功能无法实现。借鉴现有的架构部署nexus来满足权限管理等需求 思路 翻看dockerhub上没看到有编译好的arm架构的nexus&#xff0c;从github找到…

软件压力测试有多重要?北京软件测试公司有哪些?

软件压力测试是一种基本的质量保证行为&#xff0c;它是每个重要软件测试工作的一部分。压力测试是给软件不断加压&#xff0c;强制其在极限的情况下运行&#xff0c;观察它可以运行到何种程度&#xff0c;从而发现性能缺陷。 在数字化时代&#xff0c;用户对软件性能的要求越…

git 多账号配置

windows下git多账号配置详解_git配置多个用户名和密码-CSDN博客 windows下git多账号配置详解_git配置多个用户名和密码-CSDN博客 windows下git多账号配置详解_git配置多个用户名和密码-CSDN博客

学习方法该升级了,‌AI时代的弯道超车:【心流学习法】行动与意识合一的巅峰进化

你是否曾感到内心如荒漠般干涸&#xff0c;面对浩瀚的知识海洋&#xff0c;热情逐渐消磨殆尽&#xff1f; 你是否渴望忘却时间的流逝&#xff0c;心无旁骛&#xff0c;与知识展开一场纯粹而深邃的对话&#xff1f; ​在AI时代&#xff0c;智能体处理数据、知识迭代的速率让人…

手边酒店多商户版V2源码独立部署_博纳软云

新版采用laraveluniapp开发&#xff0c;为更多平台小程序开发提供坚实可靠的底层架构基础。后台UI全部重写&#xff0c;兼容手机端管理。 全新架构、会员卡、钟点房、商城、点餐、商户独立管理

Vue 3 生命周期钩子详解(setup语法糖示例)

Vue 3 生命周期钩子详解&#xff08;setup语法糖示例&#xff09; 在Vue 3的框架下&#xff0c;生命周期钩子函数得到了全面的革新&#xff0c;并被巧妙地融入了setup函数中。这些钩子函数在组件实例的生命周期的不同阶段发挥着至关重要的作用。接下来&#xff0c;我们将探讨V…