Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuex mapActions, mapGetters, etc... Mixing namespaced and non-namespace actions/getters/mutations/state in the same call?

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!

like image 965
Matt Larson Avatar asked Jan 11 '19 19:01

Matt Larson


People also ask

Are Vuex actions synchronous?

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.

How do you use mapGetters in Vuex?

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', // ... ]) } }

What is Namespaced in Vuex?

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.

What is mapActions?

mapActions is just a helper that lets you call those methods from within a Vue component.


2 Answers

Nevermind. Figured it out like so:

...mapActions({
  nsOne: 'namespaced/nsOne',
  nsTwo: 'namespace/nsTwo',
  nonNsOne: 'nonNsOne', 
  nonNsTwo: 'nonNsTwo'
})
like image 153
Matt Larson Avatar answered Sep 28 '22 00:09

Matt Larson


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']),
}
like image 20
Jason Avatar answered Sep 28 '22 01:09

Jason