1vuex简介
-
修改state状态必须通过
mutations
-
mutations
只能执行同步代码,类似ajax,定时器之类的代码不能在mutations中执行 -
执行异步代码,要通过actions,然后将数据提交给mutations才可以完成
-
state的状态即共享数据可以在组件中引用
-
组件中可以调用action
vuex基础-state
state是放置所有公共状态的属性,如果你有一个公共状态数据 , 你只需要定义在 state对象中
定义state
// 初始化vuex对象 const store = new Vuex.Store({ state: { // 管理数据 count: 0 } })
原始形式- 插值表达式
App.vue
组件中可以使用 this.$store 获取到vuex中的store对象实例,可通过state属性属性获取count, 如下
<div> state的数据:{{ $store.state.count }}</div>
计算属性 - 将state属性定义在计算属性中
// 把state中数据,定义在组件内的计算属性中 computed: { count () { return this.$store.state.count } }
<div> state的数据:{{ count }}</div>
辅助函数 - mapState
mapState是辅助函数,帮助我们把store中的数据映射到 组件的计算属性中, 它属于一种方便用法
用法 : 第一步:导入mapState
import { mapState } from 'vuex'
第二步:采用数组形式引入state属性
mapState(['count'])
上面代码的最终得到的是 类似
count () { return this.$store.state.count }
第三步:利用延展运算符将导出的状态映射给计算属性
computed: { ...mapState(['count']) }
<div> state的数据:{{ count }}</div>
vuex基础-mutations
state数据的修改只能通过mutations,并且mutations必须是同步更新,目的是形成数据快照
数据快照:一次mutation的执行,立刻得到一种视图状态,因为是立刻,所以必须是同步
定义mutations
const store = new Vuex.Store({ state: { count: 0 }, // 定义mutations mutations: { } })
格式说明
mutations是一个对象,对象中存放修改state的方法
mutations: { // 方法里参数 第一个参数是当前store的state属性 // payload 载荷 运输参数 调用mutaiions的时候 可以传递参数 传递载荷 addCount (state) { state.count += 1 } },
如何在组件中调用mutations
原始形式-$store
新建组件child-a.vue,内容为一个button按钮,点击按钮调用mutations
<template> <button @click="addCount">+1</button> </template> <script> export default { methods: { // 调用方法 addCount () { // 调用store中的mutations 提交给muations // commit('muations名称', 2) this.$store.commit('addCount', 10) // 直接调用mutations } } } </script>
带参数的传递
addCount (state, payload) { state.count += payload } this.$store.commit('addCount', 10)
辅助函数 - mapMutations
mapMutations和mapState很像,它把位于mutations中的方法提取了出来,我们可以将它导入
import { mapMutations } from 'vuex' methods: { ...mapMutations(['addCount']) }
上面代码的含义是将mutations的方法导入了methods中,等同于
methods: { // commit(方法名, 载荷参数) addCount () { this.$store.commit('addCount') } }
此时,就可以直接通过this.addCount调用了
<button @click="addCount(100)">+100</button>
但是请注意: Vuex中mutations中要求不能写异步代码,如果有异步的ajax请求,应该放置在actions中
vuex基础-actions
state是存放数据的,mutations是同步更新数据,actions则负责进行异步操作
定义actions
actions: { // 获取异步的数据 context表示当前的store的实例 可以通过 context.state 获取状态 也可以通过context.commit 来提交mutations, 也可以 context.diapatch调用其他的action getAsyncCount (context) { setTimeout(function(){ // 一秒钟之后 要给一个数 去修改state context.commit('addCount', 123) }, 1000) } }
原始调用 - $store
addAsyncCount () { this.$store.dispatch('getAsyncCount') }
传参调用
addAsyncCount () { this.$store.dispatch('getAsyncCount', 123) }
辅助函数 -mapActions
actions也有辅助函数,可以将action导入到组件中
import { mapActions } from 'vuex' methods: { ...mapActions(['getAsyncCount']) }
直接通过 this.方法就可以调用
<button @click="getAsyncCount(111)">+异步</button>
vuex基础-getters
除了state之外,有时我们还需要从state中派生出一些状态,这些状态是依赖state的,此时会用到getters
例如,state中定义了list,为1-10的数组,
state: { list: [1,2,3,4,5,6,7,8,9,10] }
组件中,需要显示所有大于5的数据,正常的方式,是需要list在组件中进行再一步的处理,但是getters可以帮助我们实现它
定义getters
getters: { // getters函数的第一个参数是 state // 必须要有返回值 filterList: state => state.list.filter(item => item > 5) }
使用getters
原始方式 -$store
<div>{{ $store.getters.filterList }}</div>
辅助函数 - mapGetters
computed: { ...mapGetters(['filterList']) }
<div>{{ filterList }}</div>
Vuex中的模块化-Module
应用
定义两个模块 user 和 setting
user中管理用户的状态 token
setting中管理 应用的名称 name
模块化中的命名空间
命名空间 namespaced
默认情况下,模块内部的 action、mutation 和 getter 是注册在全局命名空间的——这样使得多个模块能够对同一 mutation 或 action 作出响应。
通过mapMutations调用
methods: { ...mapMutations(['updateToken']) } <button @click="updateToken">修改token</button>
高封闭性?可以理解成 一家人如果分家了,此时,你的爸妈可以随意的进出分给你的小家,你觉得自己没什么隐私了,我们可以给自己的房门加一道锁(命名空间 namespaced),你的父母再也不能进出你的小家了
如
user: { namespaced: true, state: { token: '12345' }, mutations: { // 这里的state表示的是user的state updateToken (state) { state.token = 678910 } } },
使用带命名空间的模块 action/mutations
方案1:直接调用-带上模块的属性名路径
test () { this.$store.dispatch('user/updateToken') // 直接调用方法 }
方案2:辅助函数-带上模块的属性名路径
methods: { ...mapMutations(['user/updateToken']), test () { this['user/updateToken']() } } <button @click="test">修改token</button>
方案3: createNamespacedHelpers 创建基于某个命名空间辅助函数
import { mapGetters, createNamespacedHelpers } from 'vuex' const { mapMutations } = createNamespacedHelpers('user') <button @click="updateToken">修改token2</button>