Access to getter inside mutation
See original GitHub issueHi, I have the following pattern:
import { VuexModule, Module, Mutation } from 'vuex-module-decorators'
import { Investigation, InvestigationId } from './types'
@Module({ name: 'investigations' })
class Investigations extends VuexModule {
investigations: Investigation[] = []
currentInvestigationIndex = -1
get current (): Investigation | undefined {
return this.currentInvestigationIndex >= 0 ? this.investigations[this.currentInvestigationIndex] : undefined
}
get id () {
return (id: InvestigationId): Investigation | undefined => this.investigations.find(i => i.id === id)
}
@Mutation
newSequence ({ investigationId }: { investigationId: InvestigationId }): number {
const investigation = this.id(investigationId)
if (investigation === undefined) {
throw Error(`Investigation not found: ${investigationId}`)
}
return investigation.sequences.push({})
}
}
However, when accessing to newSequence
, it seems that this.id
is undefined
. This is not consistent with the behavior with getters nor actions (where it’s possible to use context).
Am I missing something?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7
Top Results From Across the Web
Accessing getters within Vuex mutations - Stack Overflow
But, any logic that needs to reference getters can run in an action, which has read access to getters and state.
Read more >[Feauture] Getters accessible in mutations #684 - vuejs/vuex
An easy, immediate solution would be to pass the getter result into the mutation, and mutate that. It will be the same objects...
Read more >Vue.js – Accessing getters within Vuex mutations - iTecNote
Vuex store mutation methods do not provide direct access to getters. ... * It is best practice to keep mutations a simple as...
Read more >Mutations | Vuex
The only way to actually change state in a Vuex store is by committing a mutation. Vuex mutations are very similar to events:...
Read more >[Solved]-Accessing getters within Vuex mutations-Vue.js
But, any logic that needs to reference getters can run in an action, which has read access to getters and state. Then the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In fact, I encountered the exact same issue with vuex-class-component, except that I can use a workaround (with proxies).
See my comment on this: it seems that Vuex itself renders this usage quite complicated.
I does also not work between getters, doesn’t it?