vuex辅助函数的用法是什么?你知道哪些?
Admin 2021-09-11 群英技术资讯 1188 次浏览
vuex辅助函数的用法是什么?vue中辅助函数有mapState、mapActions、mapMutations等等,对新手来说可能不是很了解vuex辅助函数的使用,因此这篇文章就给大家来介绍一下vuex辅助函数。
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', // ... ]) } }
总结:
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)` ]), }
总结
在不使用辅助函数的时候,
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辅助函数有帮助,想要了解更多可以继续浏览其他相关的文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
js替换字符串两端的空格,逗号,或其它任何的符号其实是很简单的,但为什么要在这儿讲呢?有一个问题是,如果字符串两端是一个或多个逗号,有可能只有一端有逗号,这种情况该怎么用正则表达式来解决呢? 下面我列举几种情况,例如将下面这些字符的两端符号去
Promise是异步编程的一种解决方案,在ES6中Promise被列为了正式规范,统一了用法,原生提供了Promise对象。接下来通过本文给大家介绍Promise的介绍及基本用法,感兴趣的朋友一起看看吧
这篇文章主要为大家详细介绍了JavaScript实现二级菜单的制作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
背景<img>加载错误解决方案内联事件全局img添加事件利用error事件捕获替换src方式的最优解CSS处理的最优解<img>加载超时解决方案嗅探切换Domain(CNAME)服务端下发Domai
箭头函数排在第一个是因为它的 this 不会被改变,所以只要当前函数是箭头函数,那么就不用再看其他规则了。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008