As I utilize the created lifecycle hook within vue.js to fetch data from my store and pass it to a vue component's data, an interesting observation arises. When I employ
this.selectedType = store.state.selectedType
, the data is successfully retrieved from the store. However, when attempting to retrieve the same data using a getter (i.e. this.selectedType = store.getters.getType()
), an error is encountered:
Error in created hook: "TypeError: Cannot read property 'selectedType' of undefined"
The confusion sets in as selectedType should not be considered undefined since it holds the value "Item"
within the store and can be correctly accessed during creation utilizing
this.selectedType = store.state.selectedType
.
A snippet of the getter function looks like this:
getters: {
getSelectedType: state => {
return state.selectedType
}
}
Moving on to how the state is set up:
state: {
selectedType: "Item"
}
If anyone could shed light on why this discrepancy exists, I suspect there may be a gap in my understanding of lifecycles contributing to this unexpected behavior.