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

3

6

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!

Heterogony answered 11/1, 2019 at 19:6 Comment(0)
H
13

Nevermind. Figured it out like so:

...mapActions({
  nsOne: 'namespaced/nsOne',
  nsTwo: 'namespace/nsTwo',
  nonNsOne: 'nonNsOne', 
  nonNsTwo: 'nonNsTwo'
})
Heterogony answered 11/1, 2019 at 19:11 Comment(0)
C
8

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']),
}
Conciliator answered 11/1, 2019 at 21:39 Comment(0)
S
0

in my case whenever I do the CRUD, I also create the states, actions, getters with default names and put a prefix to reuse the code, if I need to copy it, create a crud screen, everything is ready and I just change it. So, the names of the Actions and Getters are the same, I need to do it that way here.

methods: {
...mapActions('especialidades', ['actGetList', 'actInsert', 'actUpdate']),
...mapActions('profissionais', { profActionList: 'actGetList' }),
...mapActions('convenios', { convActionList: 'actGetList' }),
...mapActions('especialidades', { especActionList: 'actGetList' }),
Sephira answered 17/5, 2023 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.