I created an info-bar, an area I want to update with info from the component. I added it as a child of App.vue :
<template>
<div id="app">
<InfoBar /> // my info-bar
<router-view/>
</div>
</template>
To be able to update m<InfoBar /> from other components, I decided to try using Vuex and use mutations to change info:
Vuex Store:
export const store = new Vuex.Store({
state:{
infoBarText: "Text from Vuex store" , // initial text for debugging
},
mutations:{
setInfoBarText(state,text){
state.infoBarText = text;
}
}
infobar.vue
<template>
<div>
{{infoString}} // the result is always "Text from Vuex store"
</div>
</template>
<script>
export default {
name: "infoBar",
data() {
return {
infoString: this.$store.state.infoBarText
}
}
Now, I would like to update the text using the Vuex mutation from other component:
other.vue:
mounted() {
this.$store.commit("setInfoBarText", "Text from Component");
}
I checked the state of infoBarText with Vue developer tools and it successfully changed to "Text from Component" but it's not changed the text in the component.
What I am doing wrong?