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
随机推荐
- 结对编程--四则运算(Java)萧英杰 夏浚杰
结对编程--四则运算(Java)萧英杰 夏浚杰 Github项目地址 功能要求 题目:实现一个自动生成小学四则运算题目的命令行程序 使用 -n 参数控制生成题目的个数(实现) 使用 -r 参数控制题目 ...
- 使用Visual Studio Team Services敏捷规划和项目组合管理(六)——VSTS仪表盘的使用
使用Visual Studio Team Services敏捷规划和项目组合管理(六)--VSTS仪表盘的使用 仪表盘使团队能够看到项目的状态和监控项目的进展.简单来说,不必深入到团队项目站点的其他部 ...
- windows网络编程中文 笔记(二)
IPv4 地址段 IPv4地址类别 种类 网络部分 第1个数字 端点数字 A 8位 0-127 16777216 B 16位 128-191 65526 C 24位 193-223 ...
- shell编程—简介(一)
1.shell概念 shell是一个用C语音编写的程序,他是用户使用Linux的桥梁 shell既是一种命令语音,又是一种程序设计语音 shell是指一种应用程序,这个应用程序提供一个界面,用户通过这 ...
- 洗礼灵魂,修炼python(67)--爬虫篇—cookielib之爬取需要账户登录验证的网站
学完前面的教程,相信你已经能爬取大部分的网站信息了,但是当你爬的网站多了,你应该会发现一个新问题,有的网站需要登录账户才能看到更多的信息对吧?那么这种网站怎么爬取呢?这些登录数据就是今天要说的——co ...
- scrapy实例:爬取中国天气网
1.创建项目 在你存放项目的目录下,按shift+鼠标右键打开命令行,输入命令创建项目: PS F:\ScrapyProject> scrapy startproject weather # w ...
- How to monitor tempdb in MS SQL
Error: tempdb is full due to active_transaction. select ss.[host_name], ss.login_name, ss.original_l ...
- Django之--MVC的Model
在上一篇:Django之--通过MVC架构的html模板展示Hello World! 讲述了基本的MVC模型,但是却并没有测试Model的作用,本文通过mysql数据库来测试. Django自带的mo ...
- c/c++ 标准库 map multimap元素访问
标准库 map multimap元素访问 一,map,unordered_map下标操作 下标操作种类 功能描述 c[k] 返回关键字为k的元素:如果k不在c中,添加一个关键字为k的元素,并对其初始化 ...
- php学习----文件系统
PHP文件系统之读取文件内容 PHP具有丰富的文件操作函数,最简单的读取文件的函数为file_get_contents,可以将整个文件全部读取到一个字符串中. $content = file_get_ ...