vuex的简单总结使用
State负责存储整个应用的状态数据,一般需要在使用的时候在跟节点注入store对象,后期就可以使用this.$store.state直接获取状态
辅助函数的使用
1.mapState
state的mapState的辅助函数主要是为了解决
当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余。
例如当我在store.js中的state对象里面声明了以下几个属性
const store = new Vuex.Store({
state: {
orderItem: [], //订单的食物列表
orderFee: 0, //配送费
orderNum: 0, //食物数量
orderPrice: 0, //总的价格
minPrice: 0, //起送价格
cartList: [], //购物车中的商品
addressList: [], //地址集合
chooseAddress: [] //选择的地址
}
});
export default store 我如果要在一个其他的vue中使用到这个state里面的orderfee和orderNum以及orderPrice,那我就需要在computed的函数里面这样去声明
computed: {
bagnums() {
return this.$store.state.orderNum;
},
shopPrice() {
return this.$store.state.orderPrice;
},
orderFee() {
return this.$store.state.orderfee;
} }
然后在页面当中去显示
eg
<el-badge :value="bagnums" :max="99" class="item">
<i class="el-icon-goods"></i>
</el-badge> 但是这样一个个的去声明,有点太麻烦了。所以vuex中给我们增加了一个state的辅助函数mapState 我们只需要在vue中引入这个辅助函数,在computed函数中直接使用就可以了 import {mapState} from 'vuex'
computed: {
...mapstate([
'bagnums','shopPrice','orderFee'
])
}
然后页面中去显示
<el-badge :value="bagnums" :max="99" class="item">
<i class="el-icon-goods"></i>
</el-badge>
方法中去操作
mounted() {
this.addorder = this.shopPrice;
//相当于this.addorder = this.$store.state.shopPirce
//mapState通过扩展运算符将store.state.shopPirce 映射this.shopPrice 这个this 很重要,这个映射直接映射到当前Vue的this对象上。
所以通过this都能将这些对象点出来,同理,mapActions, mapMutations都是一样的道理。牢记~~~
}
2.mapMutations
state的状态属性只能通过mapMutations来进行提交。
commit:提交可以在组件中使用 this.$store.commit('xxx') 提交 mutation,或者使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(需要在根节点注入 store)。
使用commit来提交
//加入购物车并且算价格
//用mapState通过$store.commit来触发mutation
addPrice(price, food_id, name) {
this.foodId = food_id;
this.$store.commit('addPrice', { price: price, id: food_id, name: name });
this.sPrice = this.$store.state.minPrice - this.$store.state.orderPrice;
},
使用mapMutaitions来进行提交
import { mapMutations, mapState } from 'vuex'
computed: {
...mapState([
'minPrice','orderPrice'
])
}
methods(){
...mapMutations([
'addPrice' //映射在方法中的this.addPrice 相当于 this.$store.commit('addPrice')
])
//加入购物车并且算价格
//用mapMutation中的this.addPrice来触发mutation
addPrices(price, food_id, name){
this.foodId = food_id;
this.addPrice({price: price, id:food_id, name: name});
this.sPrice = this.minPrice- this.orderPrice
}
}
3.mapActions
action 类似于 mutation,不同在于:
action 提交的是 mutation,而不是直接变更状态。
action 可以包含任意异步操作。
mapActions的用法类似于mapMutations的用法
我们可以在组件中使用 this.$store.dispatch('xxx') 分发 action,或者使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用(需要先在根节点注入 store):
//使用dispatch来触发action
//从购物车减少并算价格
desPrice(price, food_id, name) {
this.$store.dispatch('adesPrice', { price: price, id: food_id, name: name });
},
//使用mapActions来触发action操作
import { mapMutations, mapState, mapActions } from 'vuex'
methods:{
...mapActions([
'adesPrice' //映射在方法中的this.adesPrice 相当于 this.$store.dispatch('desPrice')
]),
desPrice(price, food_id, name) {
this.adesPrice({ price: price, id: food_id, name: name });
},
}
store.js中的action的方法
actions: {
adesPrice(context, cartArr) {
context.commit('desPrice', cartArr);
}
}
vuex的简单总结使用的更多相关文章
- Vuex 最简单的数量增减实例
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到 Vue 的官方调试工具 ...
- vuex最简单、最直白、最全的入门文档
前言 我们经常用element-ui做后台管理系统,经常会遇到父组件给子组件传递数据,下面一个简单的例子,点击按钮,把弹框显示变量数据通过子组件的props属性传递,子组件通过$emit事件监听把数据 ...
- vuex最简单、最详细的入门文档
如果你在使用 vue.js , 那么我想你可能会对 vue 组件之间的通信感到崩溃 . 我在使用基于 vue.js 2.0 的UI框架 ElementUI 开发网站的时候 , 就遇到了这种问题 : 一 ...
- Vuex的简单应用
### 源码地址 https://github.com/moor-mupan/mine-summary/tree/master/前端知识库/Vuex_demo/demo 1. 什么是Vuex? Vue ...
- vue的挖坑和爬坑之vuex的简单入门
首先vuex的中文文档https://vuex.vuejs.org/zh-cn/ 首先vuex是什么,官方解释是 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应 ...
- NO.01---今天聊聊Vuex的简单入门
作为一款个人认为非常牛x的框架,个人使用起来得心应手,所以近期就记录一下这款框架吧. 首先说一说 Vuex 是什么? 官方给出的解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它 ...
- [转] vuex最简单、最直白、最全的入门文档
前言 我们经常用element-ui做后台管理系统,经常会遇到父组件给子组件传递数据,下面一个简单的例子,点击按钮,把弹框显示变量数据通过子组件的props属性传递,子组件通过$emit事件监听把数据 ...
- Vuex教程简单实例
什么是Vuex? vuex是一个专门为vue.js设计的集中式状态管理架构.状态?我把它理解为在data中的属性需要共享给其他vue组件使用的部分,就叫做状态.简单的说就是data中需要共用的属性. ...
- 移动端最强适配(rem适配之px2rem)&& 移动端结合Vuex实现简单loading加载效果
一.rem之px2rem适配 前言:相信许多小伙伴上手移动端时面对各式各样的适配方案,挑选出一个自己觉得简便.实用.高效的适配方案是件很纠结的事情. 深有体会... 经过多个移动端项目从最初的 vie ...
- vuex最简单的
https://segmentfault.com/a/1190000009404727 "dependencies": { "axios": " ...
随机推荐
- appium--解决中文输入不了的问题
配置 from appium import webdriver desired_caps={} desired_caps['platformName']='Android' #模拟器 desired_ ...
- [LeetCode] 919. Complete Binary Tree Inserter 完全二叉树插入器
A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...
- [LeetCode] 918. Maximum Sum Circular Subarray 环形子数组的最大和
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- [LeetCode] 363. Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [LeetCode] 213. House Robber II 打家劫舍之二
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 141. Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- oracle--18C操作指南(一)
一,安装清单 用户环境配置 查看Oracle Inventory(oraInventory)和OINSTALL组要求 您指定为Oracle Inventory目录的物理组是系统上安装的Oracle软件 ...
- springcloud(五,多个服务注册中心eureka)
spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) spring cloud (三.服务提供者demo_provid ...
- pytorch 查看中间变量的梯度
pytorch 为了节省显存,在反向传播的过程中只针对计算图中的叶子结点(leaf variable)保留了梯度值(gradient).但对于开发者来说,有时我们希望探测某些中间变量(intermed ...
- OpenShift下的JVM监控
去年写过一篇基于jmx监控的文章,这次在Openshift上实现,发现确实不少变化.主要重点问题在 1. prometheus jmx exporter的改进,不再需要运行一个独立的进程,不需要把数据 ...