I'm just curious if there's a way to mix namespaced and non-namespaced actions when you call, for example, ...mapActions
. I only have one module that is large enough to warrant full module encapsulation and thus namespacing, so some actions would be things/someAction
and some would just be someOtherAction
. I am currently mapping like so:
...mapActions('nsACtions', ['nsOne', 'nsTwo']),
...mapActions('nonNsActionOne', 'nonNsActionTwo')
but would much prefer to combine all into one mapActions
. Something like:
...mapActions('nsACtions',
['nsOne', 'nsTwo'],
'nonNsActionOne',
'nonNsActionTwo')
OR
...mapActions('nsACtions',
['nsOne', 'nsTwo'],
['nonNsActionOne',
'nonNsActionTwo'])
Neither of these examples work, so I'm curious if anyone has solved this little conundrum. Thanks!
In Vuex, actions are functions that call mutations. Actions exist because mutations must be synchronous, whereas actions can be asynchronous. You can define actions by passing a POJO as the actions property to the Vuex store constructor as shown below.
The mapGetters helper simply maps store getters to local computed properties: import { mapGetters } from 'vuex' export default { // ... computed: { // mix the getters into computed with object spread operator ... mapGetters([ 'doneTodosCount', 'anotherGetter', // ... ]) } }
In the previous Vuex tutorial, we learned that by default, getters, actions, and mutations inside modules are registered under the global namespace, which allows multiple modules to react to the same mutation or action.
mapActions is just a helper that lets you call those methods from within a Vue component.
Nevermind. Figured it out like so:
...mapActions({
nsOne: 'namespaced/nsOne',
nsTwo: 'namespace/nsTwo',
nonNsOne: 'nonNsOne',
nonNsTwo: 'nonNsTwo'
})
I've added this answer even though Matt Larson has found himself a solution which largely reflects the same thing. You can have multiple mapActions on your computed values to separate the namespaces for possible greater clarity
computed: {
mapActions('namespace', ['nsOne','nsTwo']),
mapActions(['nonNsOne','nonNsTwo']),
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With