vuex怎么使用数据(vuex的辅助函数该如何使用)
类别:编程学习 浏览量:1794
时间:2021-10-25 10:04:42 vuex怎么使用数据
vuex的辅助函数该如何使用目录
- mapState
- mapGetters
- mapMutations
- mapActios
- 多个modules
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中的名称相同时,可以传入一个数组
//定义state const state={ count:1, } //在组件中使用辅助函数 computed:{ ...mapState(['count']) }
computed:{ ...mapGetters({ // 把 `this.doneCount` 映射为 `this.$store.getters.doneTodosCount` doneCount: 'doneTodosCount' }) }
当属性名与getters中定义的相同时,可以传入一个数组
computed:{ computed: { // 使用对象展开运算符将 getter 混入 computed 对象中 ...mapGetters([ 'doneTodosCount', 'anotherGetter', // ... ]) } }
总结:
- mapState与mapGetters都用computed来进行映射
- 在组件中映射完成后,通过this.映射属性名进行使用
methods:{ ...mapMutations({ add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')` }) }
当属性名与mapMutatios中定义的相同时,可以传入一个数组
methods:{ ...mapMutations([ 'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')` // `mapMutations` 也支持载荷: 'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)` ]), }
mathods:{ ...mapActions({ add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')` }) }
当属性名与mapActios中定义的相同时,可以传入一个数组
methods:{ ...mapActions([ 'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')` // `mapActions` 也支持载荷: 'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)` ]), }
总结
- mapMutations与mapActios都在methods中进行映射
- 映射之后变成一个方法
在不使用辅助函数的时候,
this.$store.commit('app/addCount')
使用辅助函数,辅助函数的第一个参数给定命名空间的路径
computed: { ...mapState('some/nested/module', { a: state => state.a, b: state => state.b }) }, methods: { ...mapActions('some/nested/module', [ 'foo', // -> this.foo() 'bar' // -> this.bar() ]) }
或者使用 createNamespacedHelpers函数来创建一个基于命名空间的辅助函数
import { createNamespacedHelpers } from 'vuex' const { mapState, mapActions } = createNamespacedHelpers('some/nested/module') //给定路径 //使用方法与之前相同 export default { computed: { // 在 `some/nested/module` 中查找 ...mapState({ a: state => state.a, b: state => state.b }) }, methods: { // 在 `some/nested/module` 中查找 ...mapActions([ 'foo', 'bar' ]) } }
以上就是vuex的辅助函数该如何使用的详细内容,更多关于vuex的辅助函数的资料请关注开心学习网其它相关文章!
您可能感兴趣
- vuex原理及使用方法(Vuex状态机的快速了解与实例应用)
- vuex怎么使用数据(vuex的辅助函数该如何使用)
- vue-cli请求数据的方式(vue-cli配置使用Vuex的全过程记录)
- vue中的mapgetter优势(vuex 中辅助函数mapGetters的基本用法详解)
- vuex存取修改数据流程(vuex数据持久化的两种实现方案)
- vue编程加入购物车(vuex实现简单的购物车功能)
- vuex数据持续化(Vuex数据持久化实现的思路与代码)
- vue3.0零基础入门(快速掌握Vue3.0中如何上手Vuex状态管理)
- vuex-table行列转换效果(vue3+el-table实现行列转换)
- vuex中action的值怎么接(vuex中Getter的用法详解)
- 新一小兰领衔 盘点动漫中的那些 远距离恋爱情侣(盘点动漫中的那些)
- 大事件 合肥四中火了(大事件合肥四中火了)
- 翼龙贷组织出借人调研 感受鄱阳 借 来的致富路(翼龙贷组织出借人调研)
- 2023新国风戏曲教育寒假集训班汇报演出《戏娃闹元宵》图文报道(2023新国风戏曲教育寒假集训班汇报演出戏娃闹元宵图文报道)
- 九儿《狐踪谍影》出演热血女特警,戏份杀青受关注(九儿狐踪谍影出演热血女特警)
- 红色代表什么(红色代表什么寓意)
热门推荐
- css代码大全登录界面(Div+CSS仿微信公众平台登录页面)
- html5常用标记(HTML5超文本标记语言的实现方法)
- python3yield使用教程(python中yield的用法详解——最简单,最清晰的解释)
- python爬虫入门自学(自学python爬虫的建议和周期预算)
- dedecms安全设置(织梦系统DedeCMS设定栏目及文档权限登录后跳转到登陆前的页面的实现方法)
- 自建docker镜像仓库(使用docker制作分布式lnmp 镜像)
- 如何应对网站安全问题(网站安全有哪些防护措施?)
- php实现异步操作(php提供实现反射的方法和实例代码)
- react源码教程(详解React 代码共享最佳实践方式)
- django 页面传参数(django query模块)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9