一、基本用法

1. 初始化并创建一个项目

1
2
3
vue init webpack-simple vuex-demo
cd vuex-demo
npm install

2. 安装 vuex

1
npm install vuex -S

3. 在 src 目录下创建 store.js 文件,并在 main.js 文件中导入并配置

store.js 中写入

1
2
3
4
import Vue from 'vue'
//引入 vuex 并 use
import Vuex from 'vuex'
Vue.use(Vuex)

main.js 文件

1
2
3
4
5
6
7
8
9
10
import Vue from 'vue'
import App from './App.vue'
import store from './assets/store' //导入 store 对象
 
new Vue({
 //配置 store 选项,指定为 store 对象,会自动将 store 对象注入到所有子组件中,在子组件中通过 this.$store 访问该 store 对象
 store,
 el: '#app',
 render: h => h(App)
})

4. 编辑 store.js 文件

在应用 vuex 之前,我们还是需要看懂这个流程图,其实很简单。

vuex

① Vue Components 是我们的 vue 组件,组件会触发(dispatch)一些事件或动作,也就是图中的 Actions;
② 我们在组件中发出的动作,肯定是想获取或者改变数据的,但是在 vuex 中,数据是集中管理的,我们不能直接去更改数据,所以会把这个动作提交(Commit)到 Mutations 中;
③ 然后 Mutations 就去改变(Mutate)State 中的数据;
④ 当 State 中的数据被改变之后,就会重新渲染(Render)到 Vue Components 中去,组件展示更新后的数据,完成一个流程。

Vuex 的核心是 Store(仓库),相当于是一个容器,一个 Store 实例中包含以下属性的方法:

state 定义属性(状态 、数据)

store.js 中写入

1
2
3
4
5
6
7
8
9
10
11
// 定义属性(数据)
var state = {
 count:6
}
// 创建 store 对象
const store = new Vuex.Store({
 state
})
 
// 导出 store 对象
export default store;

方式1、 在 app.vue 中就能通过 this.$store 访问该 store 对象 ,获取该 count 。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<template>
 <div id="app">
 //把 count 方法直接写入,可自己执行
 <h1>{{count}}</h1>
 </div>
</template>
 
<script>
export default {
 name: 'app',
 computed:{
 count(){
  //返回获取到的数据
  return this.$store.state.count
 }
 }
}
</script>

方式2、vuex 提供的 mapGetters 和 mapActions 来访问

mapGetters 用来获取属性(数据)

① 在 app.vue 中引入 mapGetters

1
import {mapGetters} from 'vuex'

② 在 app.vue 文件的计算属性中调用 mapGetters 辅助方法,并传入一个数组,在数组中指定要获取的属性  count

1
2
3
4
5
6
7
8
9
10
<script>
import {mapGetters,mapActions} from 'vuex'
export default {
 name: 'app',
 computed:mapGetters([
 //此处的 count 与以下 store.js 文件中 getters 内的 count 相对应
 'count'
 ])
}
</script>

③ 在 store.js 中定义 getters 方法并导出

getters 用来获取属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
 
// 定义属性(数据)
var state = {
 count:6
}
// 定义 getters
var getters={
 //需要传个形参,用来获取 state 属性
 count(state){
  return state.count
 }
}
// 创建 store 对象
const store = new Vuex.Store({
 state,
 getters
})
 
// 导出 store 对象
export default store;

这样页面上就会显示传过来的数据了!接下来我们来通过动作改变获取到的数据

④在 store.js 中定义 actions 和 mutations 方法并导出

actions 定义方法(动作),可以使异步的发送请求。

commit 提交变化,修改数据的唯一方式就是显示的提交 mutations

mutations 定义变化,处理状态(数据)的改变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
 
// 定义属性(数据)
var state = {
 count:6
}
 
// 定义 getters
var getters={
 count(state){
  return state.count
 }
}
 
// 定义 actions ,要执行的动作,如流程的判断、异步请求
const actions ={
 // ({commit,state}) 这种写法是 es6 中的对象解构
 increment({commit,state}){
  //提交一个名为 increment 的变化,名字可自定义,可以认为是类型名,与下方 mutations 中的 increment 对应
  //commit 提交变化,修改数据的唯一方式就是显式的提交 mutations
  commit('increment')
 }
}
 
// 定义 mutations ,处理状态(数据) 的改变
const mutations ={
 //与上方 commit 中的 ‘increment' 相对应
 increment(state){
  state.count ++;
 }
}
// 创建 store 对象
const store = new Vuex.Store({
 state,
 getters,
 actions,
 mutations
})
 
// 导出 store 对象
export default store;

⑤ 在 app.vue 中引入 mapActions ,并调用

mapActions 用来获取方法(动作)

1
import {mapGetters,mapActions} from 'vuex'

调用 mapActions 辅助方法,并传入一个数组,在数组中指定要获取的方法 increment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<template>
 <div id="app">
 //这个 increment 方法与下面 methods 中的 increment 相对应
 <button @click="increment">增加</button>
 <button>减少</button>
 <h1>{{count}}</h1>
 </div>
</template>
 
<script>
import {mapGetters,mapActions} from 'vuex'
export default {
 name: 'app',
 computed:mapGetters([
 'count'
 ]),
 methods:mapActions([
 //该 increment 来自 store.js 中导出的 actions 和 mutations 中的 increment
 'increment',
 ])
}
</script>

这样就能通过 button 来改变获取到的 count 了。

看起来确实是挺绕的,需要在理解了原理的情况下,再细细琢磨,加深理解。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

 

原文链接:https://www.jianshu.com/p/b014ff74bdb6

/*getter是state的get方法,没有get页面就获取不到数据

获取页面:
import {mapGetters,mapActions} from 'vuex'
<h1>{{count}}</h1>
computed:mapGetters([
'count'
]), store.js: var state = {
count:6
},
var getters={
count(state){
return state.count
}
} 改变数据页面:
<button @click="increment">增加</button>
methods:mapActions([
//该 increment 来自 store.js 中导出的 actions 和 mutations 中的 increment
'increment',
]) 先发给action:
const actions ={
// ({commit,state}) 这种写法是 es6 中的对象解构
increment({commit,state}){
//提交一个名为 increment 的变化,名字可自定义,可以认为是类型名,与下方 mutations 中的 increment 对应
//commit 提交变化,修改数据的唯一方式就是显式的提交 mutations
commit('increment')
}
}
再发给mutations:
const mutations ={
//与上方 commit 中的 ‘increment' 相对应
increment(state){
state.count ++;
}
}
*/

Vuex mapGetters,mapActions的更多相关文章

  1. vuex之 mapState, mapGetters, mapActions, mapMutations 的使用

    一.介绍 vuex里面的四大金刚:State, Mutations,Actions,Getters (上次记得关于vuex笔记 http://www.cnblogs.com/adouwt/p/8283 ...

  2. vuex中的babel编译mapGetters/mapActions报错解决方法

    vex使用...mapActions报错解决办法 vuex2增加了mapGetters和mapActions的方法,借助stage2的Object Rest Operator 所在通过 methods ...

  3. vuex中的辅助函数 mapState,mapGetters, mapActions, mapMutations

    1.导入辅助函数 导入mapState可以调用vuex中state的数据 导入mapMutations可以调用vuex中mutations的方法 四个辅助函数 各自对应自己在vuex上的自己 2.ma ...

  4. 解决 vuex mapGetters 语法报错 (Unexpected token )

    在使用vuex2的mapGetters 和 mapActions 的方法时,借助 stage2 的 Object Rest Operator 特性,可以写出下面代码:  computed: { ... ...

  5. vuex使用mapActions报错解决办法

    先贴上报错: vuex2增加了mapGetters和mapActions的方法,借助stage2的Object Rest Operator 所在通过 methods:{ ...mapActions([ ...

  6. vuex mapGetters

    1.vuex 配置 //vuex的配置 //注意Store是大写 const store = new Vuex.Store({ //数据保存 state: { show: false, count: ...

  7. vuex2中使用mapGetters/mapActions报错解决方法

    解决方案 可以安装整个stage2的预置器或者安装 Object Rest Operator 的babel插件 babel-plugin-transform-object-rest-spread . ...

  8. vuex里mapState,mapGetters使用详解

    这次给大家带来vuex里mapState,mapGetters使用详解,vuex里mapState,mapGetters使用的注意事项有哪些,下面就是实战案例,一起来看一下. 一.介绍 vuex里面的 ...

  9. vuex mapActions

    在组件中使用 this.$store.dispatch('xxx') 分发 action,或者使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用( ...

随机推荐

  1. spring 第一篇(1-1):让java开发变得更简单(上)

    1.释放POJOS能量 传统开发中是如何束缚POJOS呢,如果你开发过java很长时间,那你一定有接触过EJB的开发.那时候开发一个小小的功能都要扩展框架的类或者实现其接口.所以你很容易在早期的Str ...

  2. python 开发技巧(4)-- 用PyCharm实用技巧(我自己常用的)

    pycharm快捷键 1.快速导入类 Ctrl + Alt + Space 2.追踪类和方法的定义 Ctrl + b 3.复制当前行 Ctrl + d 4.移动当前行 Ctrl + Shift + ( ...

  3. 导入mysql文件提示“ASCII '\0' appeared in the statement”

    在windows服务器上导入mysql文件时,出现以下报错:ASCII '\0' appeared in the statement, but this is not allowed unless o ...

  4. Hook length formula 学习笔记 UVALive 6625

    最近做到一个关于杨氏矩阵的题目. UVALive 6625 题目大意是用n以内的数填充杨氏矩阵,要求行严格递增,列不严格递增. 求方案数. 数据范围很小,我直接上爆搜,结果TLE了. 后来发现一位学长 ...

  5. C/C++程序开发中实现信息隐藏的三种类型

    不管是模块化设计,还是面向对象设计.还是分层设计,实现子系统内部信息的对外隐藏都是最关键的内在要求.以本人浅显的经验,把信息隐藏依照程度的不同分成(1)不可见不可用(2)可见不可用(3)可见可用. 1 ...

  6. 新MBP使用git命令时启用xcode的终端log

    Last login: Mon Oct 22 12:41:33 on consoleuser:~ me$ git Agreeing to the Xcode/iOS license requires ...

  7. android自定义View_2——Making the View Interactive

    前言:绘制出一个view只是自定义view的一个部分,还需要自定义一些view的行为,来反馈用户的使用操作,反馈的 行为要合理性,就像真是的物理世界一样,不要太玄幻哦. 可以将view的行为封装到in ...

  8. Rightscale & Amazon

    原先一直以为Rightscale是Amazno旗下的一个产品,今天才知道是Amazon partner - -||,实在汗颜. Rightscale也是一个很强大的公司,提供跨云解决方案...(呃,原 ...

  9. 添加可点击的imagebottom,有个点击动画效果

    android添加可点击的按钮,有个动画切换 ,首先定义一个 res/drawable中定义一个 btn_shutter_background.xml,其定义举例如下: <?xml versio ...

  10. 在CentOS中安装输入法

    1.需要root权限,所以要用root登录 ,或su root 2.yum install "@Chinese Support" 3.exit 4.回到桌面,system-> ...