Vuex: Add ability to access namespaced getters with dot notation

Created on 5 May 2018  路  7Comments  路  Source: vuejs/vuex

What problem does this feature solve?

When working with a namespaced module (e.g. a users module), you can access items on the user module's state like this:
this.$store.state.users.user

However, you can't access getters in the same way. This will return an error:
this.$store.getters.users.newUsers

Because getters.users is undefined. Instead you have to access it with bracket notation: this.$store.getters['users/newUsers']

This is true even though, without modules, I can access getters with dot notation:
this.$store.getters.newUsers

Is there a reason why this isn't possible with getters? It would be nice if we could access namespaced getters in the same way we can with namespaced state. This current behavior seems inconsistent.

What does the proposed API look like?

Given a users namespaced module, add the ability to access getters with: this.$store.getters.users.newUsers

proposal

Most helpful comment

why not access all actions and mutations with dot notation, isn't it simpler?
And it is more friendly to typescript.

All 7 comments

@ktsn @kazupon what's your views on this one? I can look into it during the sprint 馃檪

馃憤

+1

It's actually pretty confusing for a beginner 鈥斅營 couldn't figure out how to call a namespaced getter method up until the point I stumbled upon this ticket and it all made sense (https://vuex.vuejs.org/guide/modules.html doesn't seem to explain that at all).

I also find this pretty confusing, at least for new users. It's arguably fine once you know about it.

But a change like this would of course be backwards incompatible. However you could just keep the old names and add a deprecation notice to them, perhaps.

Anyway, yeah. :+1:

Same here... ! Just figured it out because of this ticket... my first shoot from the hip was the

getters.myNamespace.myGetter()

As mentioned above, it would be more intuitive...

why not access all actions and mutations with dot notation, isn't it simpler?
And it is more friendly to typescript.

Two years later and no updates on this?

Was this page helpful?
0 / 5 - 0 ratings