Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access vue vuex namespaced getter from template

Tags:

I have made vuex namespaced getter mapping in my .vue component like this:

...mapGetters([   'fooModule/barGetter' ]) 

How do I access this getter in the .vue component template? I have tried {{fooModule.barGetter}} but it doesn't seem to work, {{fooModule/barGetter}} is obviously wrong.

I could assign another key to the getter in mapGetters by

...mapGetters({     fooBarGetter: 'fooModule/barGetter' }) 

This allows me to access the value in the template by using {{forBarGetter}}

However, I am wondering if there is a way to access the 'fooModule/barGetter' without assigning it another key. Is it possible? if so how?

like image 953
Mohamed Hussain Avatar asked Jul 16 '17 18:07

Mohamed Hussain


People also ask

How do you access getters in mutations Vuex?

To access getters within Vuex mutations with Vue. js, we just use the state in the getter and call the getter with the state . //... const store = new Vuex.

Are the results of Vuex getter cached?

Vuex allows us to define "getters" in the store. You can think of them as computed properties for stores. As of Vue 3.0, the getter's result is not cached as the computed property does.


1 Answers

The first parameter of mapGetters can be a namespace:

computed: {     ...mapGetters("fooModule", [         "barGetter"     ]),     ...mapGetters("anotherModule", [         "someGetter"     ]) } 

That will make the value available as this.barGetter or just barGetter in templates. Note, it's perfectly acceptable to have multiple mapGetters statements.

Vuex Getters documentation

Vuex Binding helpers with namespace

like image 89
matpie Avatar answered Nov 24 '22 03:11

matpie