vue.js - Accessing getters within Vuex mutations -
within vuex store mutation, possible access getter? consider below example.
new vuex.store({ state: { question: 'is possible access getters within vuex mutation?' }, mutations: { askquestion(state) { // todo: question getter here let question = ''; if (question) { // ... } } }, getters: { getquestion: (state) => { return state.question; } } }); of course example doesn't make sense, because access question property directly on state object within mutation, hope see trying do. is, conditionally manipulating state.
within mutation, this undefined , state parameter gives access state object, , not rest of store.
the documentation on mutations doesn't mention doing this.
my guess it's not possible, unless missed something? guess alternative either perform logic outside of store (resulting in code duplication) or implementing action this, because actions have access entire store context. i'm pretty sure it's better approach, keep mutation focused on it's supposed do; mutate state. that's i'll end doing, i'm curious if accessing getter within mutation possible?
vuex store mutation methods not provide direct access getters.
this bad practice, pass reference getters when committing mutation so:
actions: { fooaction({commit, getters}, data) { commit('foo_mutation', {data, getters}) } }, mutations: { foo_mutation(state, {data, getters}) { console.log(getters); } }
Comments
Post a Comment