michaelolof / vuex-class-component

A Type Safe Vuex Module or Store Using ES6 Classes and ES7 Decorators written in TypeScript.
217 stars 21 forks source link

Accessing store instance and other properties in getters #26

Closed rollinson9 closed 5 years ago

rollinson9 commented 5 years ago

Hi,

I've been looking for a library like this for a while, it makes so much more sense to me! I've gone through the documentation (and tests) and I believe I should be able to access the store in getters and additional access other getters.

Apologies if I've missed a step, but I can recreate this issue with namespaced modules below.

@Module({ namespacedPath: 'test/' })
class TestStore extends VuexModule {
  public num = 100;

  public get biggerNum() {
    return this.num * 100;
  }

  public get biggestNum() {
    console.log(this.biggerNum /* Undefined */, this.$store /* undefined */);
    return this.biggerNum * this.$store.state.globalNum * 100;
  }
}

export const store = new Vuex.Store({
  modules: {
    test: TestStore.ExtractVuexModule(TestStore),
  },
  state: {
    globalNum: 10
  },
  mutations: {},
  actions: {},
});

// This can be imported in vue components and used for type safety
export const vxm = {
  test: TestStore.CreateProxy(store, TestStore),
};

console.log(vxm.test.biggestNum);

Thanks!

michaelolof commented 5 years ago

Hello sorry for the late reply.

this.biggerNum is undefined because you're trying to access a getter from another getter.

Remember Vuex getters only have access to the state object.

rollinson9 commented 5 years ago

Hi @michaelolof,

It's no problem, thank you for the reply!

I'm not sure if that's right? I've had no problem using the normal vuex way of access getters within getters. The first argument is state, but then the second argument is generally the getters object. Within namespaced modules, the 3rd and 4th argument are rootState and rootGetters

You should be able to see what I mean here: https://vuex.vuejs.org/guide/getters.html#property-style-access

const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, text: '...', done: true },
      { id: 2, text: '...', done: false }
    ]
  },
  getters: {
    doneTodos: state => {
      return state.todos.filter(todo => todo.done)
    },
    doneTodosCount: (state, getters) => {
      return getters.doneTodos.length
    }
  }
})

https://vuex.vuejs.org/guide/modules.html#module-local-state

const moduleA = {
  // ...
  getters: {
    sumWithRootCount (state, getters, rootState, rootGetters) {
      return state.count + rootState.count
    }
  }
}

I hope this helps to explain what I mean, if this isn't a feature you're looking to implement then please let me know.

Thank you for you help!

DanCardin commented 5 years ago

We've also run into this problem. We make use of the 2nd getter param fairly extensively.

Any chance you'd consider (reopening this issue, and) fixing this or accepting a PR which adds support for it?

To a lesser extend we also use rootState and rootGetters, but we could probably live without that.