nodejs-- vuex中mapActions

时间:2023-03-08 23:18:45
nodejs--  vuex中mapActions

mapActions() 返回的是一个对象, 用了 ... 扩展符后,才可以放进一个对象里,和其他组件内定义的 method 在同一个 methods 对象。

{
methods: mapActions() // 如果没有其它组件内的定义的方法,可以这样写
}
{
methods: {
...mapActions(),// 如果有其他定义的方法
otherMethod1 () {},
otherMethod2 () {}
}

}

假设mapActions(),返回的是

{
a() {},
b() {}
}

那 ...mapActions(),只不过是把a,b都拿出来跟其他方法放在一起了而已。
...代表两种意思,一种是剩余操作符,一种是扩展运算符,你题目里用的那个应该是剩余操作的意思,而...mapActions才是扩展运算符。

其中参数的使用方法原理为::

methods: {
'some/nested/module/foo': (val) {
return this.$store.dispatch('some/nested/module/foo', val))
}
}

但这个Mutations这么长, 一般不会这样去转换,会加个别名

methods: {
...mapActions({
foo: 'some/nested/module/foo',
bar: 'some/nested/module/bar'
})
} //相当于下面的写法 methods: {
foo(val){
return this.$store.dispatch('some/nested/module/foo', val))
}
//bar 省略....
})
}