I would like to set the initial state of a data variable inside a component from the vuex store.
However, I do the api call to update the vuex store from the component to ensure that the data is up to date.
So in my component I have the following which initiates an update
created: function () {
uibuilder.send('getSchedules')
},
the message is returned from my API and this is picked up in my main index.js:
this.$store.commit('schedules/SAVE_SCHEDULES', newVal.payload);
and the update happens by a mutation in the store.js
SAVE_SCHEDULES(state, schedules) {
state.schedules = schedules;
},
The data that is pulled in is to be used to populate the initial value of a textbox in the component. I thought that I could set a computed property to access the object in the vuex state
computed: {
schedule() {
return this.$store.getters['schedules/getScheduleByName'](this.picked);
},
},
and then set the data object with the value from the computed object.
data() {
return {
picked: 'shd-1',
tag: this.schedule.tag
}
},
I can then use v-model to keep track of changes to the value of the textbox.
<input type="text" id="shd-tag" class="mb-2" v-model="tag">
However, no matter where I put the API call in the lifecycle of the component I get an undefined value error:
Error in data(): "TypeError: Cannot read property 'tag' of undefined"
It seems that the vuex store is not updated before I try to access it in the data definition. Does anyone have any ideas how I could achieve this please?
Thanks,
Martyn
this.picked, that you send as argument to your getter, what is? Is not in the component data() declaration, are you passing it as prop?tagproperty of the return object create a computed for that too, and inside it put a logic to check if the object is instantiated.