vuex mapState、mapGetters、mapActions、mapMutations的使用
例子:
index.js

import Vue from 'vue'
import Vuex from 'vuex'
import mutations from './mutations'
import actions from './action'
import getters from './getters' Vue.use(Vuex) const state = {
userInfo: { phone: 111 }, //用户信息
orderList: [{ orderno: '1111' }], //订单列表
orderDetail: null, //订单产品详情
login: false, //是否登录
} export default new Vuex.Store({
state,
getters,
actions,
mutations,
})


computed: {
...mapState([
'orderList',
'login'
]),
},
mounted(){
console.log(typeof orderList); ==>undefind
console.log(typeof this.orderList)==>object
}

mapState通过扩展运算符将store.state.orderList 映射this.orderList 这个this 很重要,这个映射直接映射到当前Vue的this对象上。
所以通过this都能将这些对象点出来,同理,mapActions, mapMutations都是一样的道理。牢记~~~
import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex) export default new Vuex.Store({
state:{
data:'test'
},
getters:{ },
mutations:{ },
actions:{ }
})
<template>
<div id="app">
{{count}}
//{{data}}
</div>
</template>
<script>
//想要使用 首先需要按需引入
import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'
export default {
// 通过对象展开运算符将count混入computed对象中,作为computed对象的方法使用
computed:{
//相当于
// count(){
// return this.$store.state.data
// }
//采用对象方式相当于重命名
...mapState({
count: 'data'
})
//采用数组方式
//...mapState([data])
//可在其他钩子中使用this.data调用
}
//其他mapGetters,mapMutations,mapActions原理一样
}
</script>
<style> </style>
另外mapState通过扩展运算符将store.state.data映射this.count 这个this 很重要,这个映射直接映射到当前Vue的this对象上。
在钩子函数中可直接 this.count调用
当state,mutations,actions数量很多时,在一个文件夹下不方便管理。可把state,getters,mutations,actions分别写在不同文件内,通过export 导出。再在一个文件中引入。
例如:
//state.js
const state={
count:10
}
export default state; //切记记得导出
//getters.js
export const gets= state => state.count
//这里直接导出 就不用export default
相当于 gets=function(state){
return state.count
}
//mutations.js
const mutations={
add(state){
state.count+=1
},
sub(state){
state.count-=1
}
} export default mutations;
//actions.js
export const addactions=(context)=>{
context.commit('add')
}
export const subactions=(context)=>{
context.commit('sub')
}
所有文件导出后在一个文件内统一引入
//store文件夹下的store.js
import Vue from 'vue'
import Vuex from 'vuex' import state from './state.js'
//由于上边的getters.js 和actions.js是通过export 导出每一个,
import * as getters from './getters.js'
import mutations from './mutations.js'
import * as actions from './actions.js' Vue.use(Vuex) export default new Vuex.Store({
state,
getters,
mutations,
actions
})
使用
<template>
<div id="app">
<div>数量{{count}}</div>
<div @click="add">增加</div>
<div @click="sub">减少</div>
</div>
</template> <script>
import {mapState,mapGetters,mapMutations,mapActions} from 'vuex' export default {
computed:{
//注意这里可以直接使用,因为通过...mapxxx引进来,因为是state,所以可以直接当对象的属性使用,若是mutations,actions则当对象的方法使用。
...mapState(["count"]) },
methods:{
...mapMutations(["add","sub"])
}
}
</script>
vuex mapState、mapGetters、mapActions、mapMutations的使用的更多相关文章
- vuex之 mapState, mapGetters, mapActions, mapMutations 的使用
一.介绍 vuex里面的四大金刚:State, Mutations,Actions,Getters (上次记得关于vuex笔记 http://www.cnblogs.com/adouwt/p/8283 ...
- vuex中的辅助函数 mapState,mapGetters, mapActions, mapMutations
1.导入辅助函数 导入mapState可以调用vuex中state的数据 导入mapMutations可以调用vuex中mutations的方法 四个辅助函数 各自对应自己在vuex上的自己 2.ma ...
- Vue 状态管理之vuex && {mapState,mapGetters}
1 # 一.理解vuex 2 1.概念:专门在Vue中实现集中式状态(数据)管理的一个Vue插件,对vue应用中多个组件的共享状态进行集中式的管理(读写),也是一种组件间通信的方式,且适用于任意组件间 ...
- vuex里mapState,mapGetters使用详解
这次给大家带来vuex里mapState,mapGetters使用详解,vuex里mapState,mapGetters使用的注意事项有哪些,下面就是实战案例,一起来看一下. 一.介绍 vuex里面的 ...
- vuex 的使用 mapState, mapGetters, mapMutations, mapActions
state => 基本数据getters => 从基本数据派生的数据mutations => 提交更改数据的方法,同步!actions => 像一个装饰器,包裹mutation ...
- Vuex mapGetters,mapActions
一.基本用法 1. 初始化并创建一个项目 ? 1 2 3 vue init webpack-simple vuex-demo cd vuex-demo npm install 2. 安装 vuex ? ...
- 理解Vuex的辅助函数mapState, mapActions, mapMutations用法
在讲解这些属性之前,假如我们项目的目录的结构如下: ### 目录结构如下: demo1 # 工程名 | |--- dist # 打包后生成的目录文件 | |--- node_modules # 所有的 ...
- [转]理解Vuex的辅助函数mapState, mapActions, mapMutations用法
原文地址:https://www.cnblogs.com/tugenhua0707/p/9794423.html 在讲解这些属性之前,假如我们项目的目录的结构如下: ### 目录结构如下: demo1 ...
- mapState ,mapGetters ,mapMutations,mapActions
参考 http://www.imooc.com/article/14741
随机推荐
- 使用VSTS的Git进行版本控制(三)——评审历史记录
使用VSTS的Git进行版本控制(三)--评审历史记录 Git使用存储在每个提交中的父引用信息来管理开发的完整历史记录.评审该提交历史记录,能够找出文件更改的时间,并确定代码版本之间的差异. Git使 ...
- Could not update the distribution database subscription table. The subscription status could not be changed.
在一个测试服务器删除发布(Publication)时遇到下面错误,具体如下所示 标题: Microsoft SQL Server Management Studio --------------- ...
- spring4笔记----spring4构造注入
与设值注入有以下不同,颜色标出 package com.ij34.web; import com.ij34.servce.people; import com.ij34.servce.root; pu ...
- css把容器级别(div...)标签固定在一个位置(在页面最右边)
.process{ border:1px solid #B7B7B8; background:#F8F8F8; width:80px; height:250px; <!--固定定位; text- ...
- OpenSSL 正确安装
经过几天的各种尝试,总算正常安装了openssl,中途差点各种放弃,最后总算装好了. 环境:Win10 , 为了装OpenSSL 而安装了vs2010,没有验证必须要装的 安装步骤: .从openss ...
- C#从http上拿返回JSON数据
C#如何拿到从http上返回JSON数据? 第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化对象) ...
- 高通平台如何使用QPST抓DUMP
一 :确认手机状态 手机系统死机白屏后,使用USB线 连接手机和计算机.打开计算机设备管理器 ,当其中与手机相关的端口只有DIAG 口 项(9006端口)时,表明手机处于DUMP 模式,可以抓DUMP ...
- c/c++ 标准库 map set 插入
标准库 map set 插入 一,插入操作 有map如下: map<string, size_t> cnt; 插入方法: 插入操作种类 功能描述 cnt.insert({"abc ...
- 计数排序与桶排序python实现
计数排序与桶排序python实现 计数排序 计数排序原理: 找到给定序列的最小值与最大值 创建一个长度为最大值-最小值+1的数组,初始化都为0 然后遍历原序列,并为数组中索引为当前值-最小值的值+1 ...
- mysql+centos7+主从复制
MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的 ...