位置: IT常识 - 正文

【vue2】vuex的安装、配置与使用(怎么安装vue2.0)

编辑:rootadmin
【vue2】vuex的安装、配置与使用 一、前言

推荐整理分享【vue2】vuex的安装、配置与使用(怎么安装vue2.0),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vuex怎么安装,vue的安装,vue2.0安装,vue安装vuex,vue安装vuex,vue安装vuex,vue2.0安装,vuex安装 报错,内容如对您有帮助,希望把文章链接给更多的朋友!

使用vuex可以实现数据的共享。

二、安装

vscode中新建终端安装vuex。由于vue2不能使用vuex4的版本,所以在安装时需要指定版本3

npm i vuex@3三、vuex工作流

vuex核心包括actions、mutations、state。

①state用来存储数据;

②actions用来响应组件的事件,也可以对数据进行加工,或者进行后端请求,也就是说组件中调用dispatch方法,可以触发actions中的方法;

③mutations用来操作state,actions中调用commit方法来调用mutations。

④其他:当不需要对数据进行额外加工的时候,可以直接在组件中调用commit方法触发mutations中的方法

四、配置

步骤一:新建文件夹store,文件夹下新建index.js文件

步骤二:index.js中完成配置:还没有配置数据和事件

import Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)// 用来存储数据const state = {}// 响应组件中的事件const actions = {}// 操作数据const mutations = {}// 用来将state数据进行加工const getters = {}// 新建并暴露storeexport default new Vuex.Store({ state, actions, mutations, getters})

步骤三:main.js中引入

import Vue from 'vue'import App from './App.vue'import store from './store/index';Vue.config.productionTip = falsenew Vue({ render: h => h(App), store}).$mount('#app')五、使用

5.1、常规写法

【vue2】vuex的安装、配置与使用(怎么安装vue2.0)

1、state

组件中使用$store.state.xxx获取值,例如:

<li v-for="p in $store.state.person" :key="p.id"> 姓名:{{ p.personName }} 年龄:{{ p.age }} <button @click="deletePerson(p.id)">删除</button></li>

vuex的index.js:

const state = { person: [ { id: nanoid(), personName: "张三", age: 18 }, { id: nanoid(), personName: "张4", age: 28 }, { id: nanoid(), personName: "张5", age: 38 }, ]}

2、getter

组件中使用$store.getters.xxx获取getters中的返回值

<span> 学生年龄总和:{{ $store.getters.getAllPersonAge }} </span>

index.js:

// 用来将state数据进行加工:类似于computedconst getters = { getAllPersonAge(state) { var sumAge = 0; state.person.forEach(element => { sumAge += element.age }); return sumAge }}

3、actions

组件中使用this.$store.dispatch("aaa", xxx)触发actions中的aaa方法,参数为xxx

<input type="text" v-model="newPerson" /><button @click="addPerson">添加</button>addPerson() { if (this.newPerson == "") { alert("请输入姓名"); return; } this.$store.dispatch("addPerson", this.newPerson); this.newPerson=""},

index.js

// 响应组件中的事件const actions = { //添加人员 addPerson(content, value) { console.log(value) const person = { id: nanoid(), personName: value, age: 18//暂时写死 } content.commit("addPerson", person) }}

4、mutations

组件中:

<button @click="deletePerson(p.id)">删除</button>deletePerson(id) { this.$store.commit("deletePerson", id);},

index.js

const mutations = { addPerson(_, value) { this.state.person.unshift(value) }, deletePerson(_, id) { const newArr = this.state.person.filter(p => { return p.id != id }) this.state.person = newArr }}

5.2 四个map写法

上述写法都需要手动写this.$store.xxx比较麻烦,可以在组件中引入vuex的四个map,简化操作

组件中引入:

import { mapState, mapActions, mapGetters, mapMutations } from "vuex";

使用:

computed: { ...mapState(["person"]), ...mapGetters(["getAllPersonAge"]),},methods: { ...mapActions({ addPerson1: "addPerson", //第一个为本地方法名,第二个参数为actions中的方法名}), ...mapMutations(["deletePerson"]),//当本地和index.js中的方法名一致时,可以简化成数组写法},

使用:直接使用上面中定义的参数名

<input type="text" v-model="newPerson" /> <button @click="addPerson1(newPerson)">添加</button> <ul> <li v-for="p in person" :key="p.id"> 姓名:{{ p.personName }} 年龄:{{ p.age }} <button @click="deletePerson(p.id)">删除</button> </li> </ul> <span> 学生年龄总和:{{ getAllPersonAge }} </span>五、其他

以上就是vuex的所有介绍,我们一起进步。

本文链接地址:https://www.jiuchutong.com/zhishi/290785.html 转载请保留说明!

上一篇:阿尔高阿尔卑斯山脉的Schrecksee湖,德国巴伐利亚 (© Andreas Hagspiel/EyeEm/Getty Images)(阿尔卑斯山百度百科)

下一篇:JavaScript includes() 方法

  • 机票抵扣进项税怎么填申报表
  • 利率 税率
  • 员工买东西自己垫付的钱怎么做账
  • 计提坏账所得税费用分录
  • 行政单位设计费计入哪个科目
  • 淘宝买货没有发票
  • 应收票据资产负债表
  • 怎么开具红票
  • 一般纳税人注销需要多少钱
  • 当留底税额大于退税额会计分录怎么写?
  • 付给其他公司的利息怎么做账
  • 企业收到政府补助要交税吗
  • 初始数据试算表不平衡的原因
  • 别人垫付的医药费,可以报销吗
  • 营改增后餐饮业税率
  • 零税率发票可以冲成本吗
  • 城建税及教育费附加税怎么算
  • 员工旅游费需要缴纳个人所得税吗
  • 税控盘减免在哪里填报
  • 特定行业如何界定
  • 未抵扣的进项税可以留底多久
  • 增值税5%的税率怎么算
  • 研发费用费用化金额和资本化金额有什么区别
  • 代开普通发票要什么材料?
  • 收到工会经费返还属于现金流量表哪
  • 安卓系统详解
  • 社保是当月计提当月的吗
  • PHP:oci_set_client_info()的用法_Oracle函数
  • PHP:stream_context_get_options()的用法_Stream函数
  • PHP:pg_field_size()的用法_PostgreSQL函数
  • 缴医保分录
  • 出国考察是什么意思
  • 摊余成本计量的金融资产若溢价购买小于
  • 零售商品盘点表填写内容
  • php请求https
  • framework启动
  • 商业连锁企业有哪些
  • 矿产资源补偿费计入管理费用吗
  • 资产负债表是不是根据记账凭证生成的
  • Win11 Build 23435 预览版今日发布: 文件管理器引入图库功能
  • 亏损企业所得税怎么交
  • javaweb购物车设计报告
  • 遥感图像超分辨率 坑
  • node.js deno
  • 免征的教育费附加怎么做账
  • Vite + Vue2 + Vuetify2 + <script setup> + TypeScript 搭配开发项目
  • 自动执行python
  • dedecms怎么改图片
  • 国债 企业
  • 过渡期怎么坚持下去
  • 社保怎么缴费方式
  • 租赁合同印花税率多少
  • PostgreSQL中的OID和XID 说明
  • windows下重启mysql服务
  • 公司组织出国旅游很差吗
  • 关于发放节日补助的通知
  • 营改增后土地出让增值税
  • 原材料转固定资产账务处理
  • 对公账户是不是哪个银行都可以
  • 财务月末暂估成本是什么
  • mysql order by 性能
  • sql server中的文件位置可以很灵活
  • mysql80安装配置教程
  • 如何把数据生成表格
  • centos安装NVIDIA驱动
  • 苹果mac安装win7系统
  • windows使用svn命令
  • 打开本地搜索
  • jquery插件使用教程
  • python3的urllib
  • angularjs内置了很多有用的服务
  • node的全局变量有哪些
  • Android OpenGL ES(九)----构建几何物体
  • javascript编程基础
  • jquery 选择
  • jquery基本知识
  • python面积
  • 海关免税金额现在是多少金额
  • 我国为什么不推行安乐死
  • 当月专票作废流程
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设