读取store的字段值,经过this.$store.state访问html
const Counter = {
template: `<div>{{ count }}</div>`,
computed: {
count () {
return this.$store.state.count
}
}
}
复制代码
当一个组件须要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余。为了解决这个问题,咱们可使用 mapState 辅助函数帮助咱们生成计算属性,让你少按几回键,注意可使用别名映射vue
// 在单独构建的版本中辅助函数为 Vuex.mapState
import { mapState } from 'vuex'
export default {
// ...
computed: mapState({
// 箭头函数可以使代码更简练
count: state => state.count,
// 传字符串参数 'count' 等同于 `state => state.count`
countAlias: 'count',
// 为了可以使用 `this` 获取局部状态,必须使用常规函数
countPlusLocalState (state) {
return state.count + this.localCount
}
})
}
复制代码
当映射的计算属性的名称与 state 的子节点名称相同时,咱们也能够给 mapState 传一个字符串数组。vuex
computed: mapState([
// 映射 this.count 为 store.state.count
'count'
])
复制代码
也可使用对象展开运算符将此对象混入到外部对象中数组
localComputed () { /* ... */ },
...mapState({
// ...
})
复制代码
使用 Vuex 并不意味着你须要将全部的状态放入 Vuex。虽然将全部的状态放到 Vuex 会使状态变化更显式和易调试,但也会使代码变得冗长和不直观。若是有些状态严格属于单个组件,最好仍是做为组件的局部状态。缓存
getters: {
doneTodosCount: (state, getters) => {
return getters.doneTodos.length
}
}
复制代码
mapGetters 辅助函数仅仅是将 store 中的 getter 映射到局部计算属性,和mapState差很少,可使用别名映射bash
import { mapGetters } from 'vuex'
export default {
// ...
computed: {
// 使用对象展开运算符将 getter 混入 computed 对象中
...mapGetters([
'doneTodosCount',
'anotherGetter',
// ...
])
// 把 `this.doneCount` 映射为 `this.$store.getters.doneTodosCount`
doneCount: 'doneTodosCount'
}
}
复制代码
更改 Vuex 的 store 中的状态的惟一方法是提交 mutation。Vuex 中的 mutation 很是相似于事件:每一个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是咱们实际进行状态更改的地方,而且它会接受 state 做为第一个参数。你不能直接调用一个 mutation handler,只能store.commit('increment')异步
const store = new Vuex.Store({
state: {
count: 1
},
mutations: {
increment (state) {
// 变动状态
state.count++
}
}
})
store.commit('increment') //至关于触发事件increment
复制代码
你能够向 store.commit 传入额外的参数,即 mutation 的 载荷(payload),至关于触发事件increment,看成事件更容易理解ide
mutations: {
increment (state, n) {
state.count += n
}
}
store.commit('increment', 10)
复制代码
store.commit({
type: 'increment',
amount: 10
})
//当使用对象风格的提交方式,整个对象都做为载荷传给 mutation 函数
复制代码
你能够在组件中使用 this.$store.commit('xxx') 提交 mutation,或者使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(须要在根节点注入 store)。注意可使用别名映射,和mapGetters中同样函数
import { mapMutations } from 'vuex'
export default {
methods: {
...mapMutations([
'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`
// `mapMutations` 也支持载荷:
'incrementBy' // 将 `this.incrementBy(amount)` 映射为`this.$store.commit('incrementBy', amount)`
]),
...mapMutations({
add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')`
})
}
}
复制代码
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment (state) {
state.count++
}
},
actions: {
increment (context) {
context.commit('increment')
}
}
})
复制代码
Action 经过 store.dispatch 方法触发ui
store.dispatch('increment')
复制代码
和 mutation 差很少你在组件中使用 this.$store.dispatch('xxx') 分发 action,或者使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用(须要先在根节点注入 store)
和mapState,mapGetters,mapMutations做用一致,方便批量合并action
Vuex 容许咱们将 store 分割成模块(module)。每一个模块拥有本身的 state、mutation、action、getter、甚至是嵌套子模块,至关于建立隔离的store实例,Module内部可使用命名空间,具体看官方文档>>
const moduleA = {
state: { ... },
mutations: { ... },
actions: { ... },
getters: { ... }
}
const moduleB = {
state: { ... },
mutations: { ... },
actions: { ... }
}
const store = new Vuex.Store({
modules: {
a: moduleA,
b: moduleB
}
})
store.state.a // -> moduleA 的状态
store.state.b // -> moduleB 的状态
复制代码