vuex辅助函数的用法是什么?你知道哪些?
Admin 2021-09-11 群英技术资讯 1054 次浏览
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的相关知识,其中主要介绍了关于Math对象方法的相关问题,Math对象是JavaScript的内置对象,提供一系列数学常数和数学方法,该对象不是构造函数,所以不能生成实例,所有的属性和方法都必须在Math对象上调用,下面一起来看一下,希望对大家有帮助。
jquery实现radio单选框选中事件很简单,当我们选中radio选项的时候会触发事件,并获取radio选中的值,先用html实现radio单选按钮。 html代码如下,checked属性表示默认选中第一个,3个name属性一样的话,radio就会自动变成只能选择一项的单选按钮。 divinput
consthttp=require('http');consthostname='127.0.0.1';constport=3000;constserver=http.createServer((req,res)=>{ if(req.url!='/favicon.ico'){ //过滤favicon请求c
windows下怎么安装node版本管理工具(nvm)?下面本篇文章给大家介绍一些安装nvm工具踩的坑,以及解决方法,希望对大家有所帮助!
这篇文章主要为大家介绍了react fiber执行原理示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008