Vuex 简单使用

官网:https://vuex.vuejs.org/zh/

参考文章:https://www.cnblogs.com/chinabin1993/p/9848720.html

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。

vuex中,有默认的五种基本的对象:

  • state:存储状态(变量)
  • getters:对数据获取之前的再次编译,可以理解为state的计算属性。我们在组件中使用 $sotre.getters.fun()
  • mutations:修改状态,并且是同步的。在组件中使用$store.commit(”,params)。这个和我们组件中的自定义事件类似。
  • actions:异步操作。在组件中使用是$store.dispath(”)
  • modules:store的子模块,为了开发大型项目,方便状态管理而使用的。

新建项目,步骤省略

安装vuex:

npm install vuex --save

1.state

新建一个项目,main.js中引入新建的store.js

Vuex 简单使用

在store.js中

javascript;gutter:true; import Vue from 'vue' import Vuex from 'vuex'</p> <p>Vue.use(Vuex)</p> <p>const state = { count: 5 }</p> <p>export default new Vuex.Store({ state })</p> <pre><code> 新建Index.vue,用于练习,并在路由中引入 ![Vuex 简单使用](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230605/1638260-20210831153404859-1889822014.png) Index.vue ![Vuex 简单使用](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230605/1638260-20210831153540627-164455409.png) 可正常显示 ![Vuex 简单使用](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230605/1638260-20210831153600464-1289568744.png) ### **2.mutations** 为了操作其中的值,可以使用mutations和actions store.js中写了两个方法,操作state中的数值增加活减少 描述的有点复杂,官网解释的比较清楚:https://vuex.vuejs.org/zh/guide/mutations.html 更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数: </code></pre> <p>import Vue from 'vue' import Vuex from 'vuex'</p> <p>Vue.use(Vuex)</p> <p>const state = { count: 5 }</p> <p>const mutations = { add(state, n) { return (state.count += n) }, des(state, n) { return (state.count -= n) } }</p> <p>// const actions = { // actionAdd(context, n) { // return context.commit('add', n) // }, // actionDes({commit},n) { // commit('des', n) // } // } export default new Vuex.Store({ state, mutations,</p> <p>})</p> <pre><code> Index.vue,增加两个按钮,做增加和减少操作 你不能直接调用一个 mutation handler。这个选项更像是事件注册:"当触发一个类型为 的 mutation 时,调用此函数。"要唤醒一个 mutation handler,你需要以相应的 type 调用 store.commit 方法: 你可以向 .commit 传入额外的参数,即 mutation 的 载荷(payload): <pre><template> <div> <div> <h3>{{$store.state.count}}</h3> <span><strong> <button>add</button> <button>des</button></strong></span> </div> <!-- <div>异步操作</div>--> <!-- <div>--> <!-- <button @click="handleActionsAdd(10)">异步增加</button>--> <!-- <button @click="handleActionsReduce(10)">异步减少</button>--> <!-- </div>--> </div> </template> <script><span> export </span><span>default</span><span> { name: </span>'Index'<span>, methods:{ <span><strong>myAdd(n){ </strong></span></span><span><strong>this.$store.commit('add',n) }, myDes(n){ this.$store.commit('des'</strong></span><span><span><strong>,n) },</strong></span> handleActionsAdd(n){ </span><span>this</span>.$store.dispatch('actionAdd'<span>,n); }, handleActionsReduce(n){ </span><span>this</span>.$store.dispatch('actionDes'<span>,n); } } } </span></script> <style> </style></pre> 点击增加按钮与减少按钮均生效: ![Vuex 简单使用](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230605/1638260-20210831154630738-291079885.png) ### **3.Action** Action 类似于 mutation,不同在于: * Action 提交的是 mutation,而不是直接变更状态。 * Action 可以包含任意异步操作。 官网:https://vuex.vuejs.org/zh/guide/actions.html store.js,Action 通过 .dispatch 方法触发: </code></pre> <p>import Vue from 'vue' import Vuex from 'vuex'</p> <p>Vue.use(Vuex)</p> <p>const state = { count: 5 }</p> <p>const mutations = { add(state, n) { return (state.count += n) }, des(state, n) { return (state.count -= n) } }</p> <p>const actions = { actionAdd(context, n) { return context.commit('add', n) }, actionDes({commit},n) { commit('des', n) } } export default new Vuex.Store({ state, mutations, actions })</p> <pre><code> Index.vue,在methods中,增加两个方法,使用dispath来触发 <pre><template> <div> <div> <h3>{{$store.state.count}}</h3> <button>add</button> <button>des</button> </div> <div>异步操作</div> <div> <span><strong> <button>异步增加</button> <button>异步减少</button></strong></span> </div> </div> </template> <script><span> export </span><span>default</span><span> { name: </span>'Index'<span>, methods:{ myAdd(n){ </span><span>this</span>.$store.commit('add'<span>,n) }, myDes(n){ </span><span>this</span>.$store.commit('des'<span>,n) }, <span><strong> handleActionsAdd(n){ </strong></span></span><span><strong>this.$store.dispatch('actionAdd',n); }, handleActionsReduce(n){ this.$store.dispatch('actionDes'</strong></span><span><span><strong>,n); }</strong></span> } } </span></script> <style> </style></pre> ![Vuex 简单使用](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230605/1638260-20210831155153325-1285928395.png) ### **4.getters** 官网:https://vuex.vuejs.org/zh/guide/getters.html store.js </code></pre> <p>import Vue from 'vue' import Vuex from 'vuex'</p> <p>Vue.use(Vuex)</p> <p>const state = { count: 5 }</p> <p>const mutations = { add(state, n) { return (state.count += n) }, des(state, n) { return (state.count -= n) } }</p> <p>const actions = { actionAdd(context, n) { return context.commit('add', n) }, actionDes({commit}, n) { commit('des', n) } }</p> <p>const getters = { getCount(state) { return (state.count) } }</p> <p>export default new Vuex.Store({ state, mutations, actions, getters })</p> <pre><code> Index.vue,获取getters中的方法 </code></pre> <p>getters--count:{{this.$store.getters.getCount}}

Vuex 简单使用

简单使用就到这里,听说还有一个vuex官方给了我们一个更简单的方式来使用vuex, {mapState, mapMutations, mapActions, mapGetters}

以后再看吧

代码:gitee

Original: https://www.cnblogs.com/crazy-lc/p/15210875.html
Author: MyBeans
Title: Vuex 简单使用

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/621822/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • MySQL专题2: 事务和锁

    这属于数据库事务的基础概念了, 就是ACID Atomicity, 原子性, 事务包含的所有操作要么全部成功, 要么全部失败回滚. Consistency, 一致性, 事务执行前后…

    数据库 2023年5月24日
    0162
  • Spring Boot启动流程

    Spring Boot启动流程 君生我未生,君生我已老。君恨我生迟,我恨君生早。 一、简述 Spring Boot启动流程分析使用版本SpringBoot VERSION:版本 2…

    数据库 2023年6月14日
    0114
  • 360浏览器兼容模式下jsp页面访问不到js文件

    360浏览器兼容模式下jsp页面访问不到js文件 查看自己js中的语法问题,不要用ES6的语法,编译不了故找不到js文件 const var of 码出高效 java 所有整型包装…

    数据库 2023年6月11日
    088
  • 第16章 变量、流程控制与游标

    第16章 变量、流程控制与游标 1. 变量 在MySQL数据库的存储过程和函数中,可以使用变量来存储查询或计算的中间结果数据,或者输出最终的结果数据。 在 MySQL 数据库中,变…

    数据库 2023年6月6日
    0100
  • day04-2发送文件

    多用户即时通讯系统04 4.编码实现03 4.6功能实现-发送文件功能实现 4.6.1思路分析 客户端(发送者): 先把文件a.jpg读取到客户端的字节数组 把文件对应的字节数组封…

    数据库 2023年6月11日
    093
  • MySQL 服务无法启动。服务没有报告任何错误。

    版本8.0.25,今天启动发现抱错,网上搜索一下发现这样可行 启动MySQL报错: 搜索了一下,按照这样步骤解决了 1.配置一下my.ini [mysqld] basedir =&…

    数据库 2023年6月16日
    098
  • 了解HTML/CSS/JS/JQuery/ajax等前端知识

    什么是HTML 超文本标记语言 浏览器通过识别相应的标签来加载页面 通过HTTP协议传输,不是编程语言 HTML常用标签 title script style link meta …

    数据库 2023年6月16日
    0148
  • 国内访问github很慢或者访问不了,解决办法

    国内因为某些问题,会屏蔽国外一些网站。有时GitHub访问的时候会出现错误,无法访问。解决办法 进入https://fastly.net.ipaddress.com/(全球最好的I…

    数据库 2023年6月6日
    0138
  • mysql范式

    mysql范式: mysql建表的规范格式 第一范式:保证每列的原子性(字段不能再分解) 第一种范式是最基本的范式。如果数据库表中的所有字段值都是不可分解的原子值,则数据库满足第一…

    数据库 2023年5月24日
    0107
  • FastDFS分布式文件系统简介

    1. 什么是FastDFS FastDFS 是用 c 语言编写的一款开源的分布式文件系统。FastDFS 为互联网量身定制, 充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高…

    数据库 2023年6月14日
    0114
  • IntelliJ IDEA community 安装教程

    jetbrains官网下载 IntelliJ IDEA安装包 此处选择社区版的zip文件 下载完成后解压安装包,此处解压目录为 E:\IntelliJ IDEA\ 开始安装首先添加…

    数据库 2023年6月11日
    0116
  • 项目中所用到的mysql重复过滤

    问题:首先用户会本地上传一批号码(可能重复)到我们项目,通过解析文件,把号码入库(只验证是不是号码其他不做改动)到号码表,然后对号码进行去重操作. 表结构为:主键(id),号码(m…

    数据库 2023年6月11日
    091
  • Consul 入门-运行

    HashiCorp Consul 是由 HashiCorp 公司开发的,它是一家专注于 DevOps 工具链的公司,旗下的明星级产品包括 Vagrant、Terraform、Vau…

    数据库 2023年6月6日
    088
  • Jenkins初始化界面一直显示Please wait while Jenkins is getting ready to work …

    第一次访问 jenkins时,会提示如下界面: 注:如果这个界面初始化的时间过长,则需要修改相关配置文件。 原因:因为访问官网太慢。我们只需要换一个源,不使用官网的源即可。 1、找…

    数据库 2023年6月14日
    081
  • 设计模式之抽象工厂

    一、抽象工厂:不管是简单工厂还是工厂方法,他们创建的都是同一类对象,有时候需要一组一组的创建对象,如果需要创建一组对象,抽象工厂是最好的方式,而抽象工厂的目的旨在创建一组包含多种不…

    数据库 2023年6月14日
    089
  • Ubuntu 服务器安装 MySQL 远程数据库

    在 Web 项目中,我们需要使用到远程数据库,开发阶段也需要连接并查看数据库的状况。腾讯云、阿里云等云平台提供了远程数据库,可直接使用;当然也可以自己在部署 Web 的服务器上安装…

    数据库 2023年6月14日
    097
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球