vuex的辅助函数
vuex提供了一些非常方便的辅助函数,比如mapState、mapGetter、mapMutation、mapAction。
这些函数可以使组件快速便捷的访问到vuex的相关信息。
引入如:
vue
import { mapState } from 'vuex这些辅助函数可以以数组或者对象的方式获取,常与展开运算符一起使用,如:
javascript
...mapState([
'doneTodosCount',
'anotherGetter',
// 如果开启命名空间记得加前缀,'XXX/anotherGetter'
])
/* 或者 */
...mapState({
// 把 `this.doneCount` 映射为 `this.$store.state.doneTodosCount`
doneCount: 'doneTodosCount'
})使用vuex要注意分模块是否有命名空间,推荐使用命名空间
javascript
{
namespaced:true, // 这里是命名空间开启
state: {
},
mutations: {
},
actions: {
}
}与属性相关的辅助函数放在computed中展开,与方法有关的函数放在methods中展开。