vuex辅助函数的用法是什么?你知道哪些?
Admin 2021-09-11 群英技术资讯 1283 次浏览
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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了JavaScript时间复杂度和空间复杂度,时间复杂度和空间复杂度是衡量一个算法是否优秀的标准,通常我们比较两个算法时会用预先估算和事后统计,下文详细介绍,需要的朋友可以参考一下
这篇文章主要给大家介绍了关于Vue中ref和$refs使用方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
本文主要介绍了react hooks组件间的传值方式(使用ts),文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
JavaScript改变src路径的方法:1、利用元素对象的src属性,语法“元素对象.src=新路径地址;”;2、利用setAttribute()方法,语法“元素对象.setAttribute(src,新路径地址);”。 本教程操作环境:
这篇文章给大家分享的是jquery怎样实现图片悬浮的效果的内容,图片悬浮是一个比较常见的图片展示效果,也就是点击小图,然后悬浮展现的大图的一个效果,小编觉得挺实用的,因此分享给大家做个参考,感兴趣的朋友一起跟随小编看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008