I have the following layout of my vue app:
App.vue
<template>
<div id="app">
<Progress />
<router-view />
<Footer />
</div>
</template>
<script>
import Footer from "@/components/Footer";
import Progress from "@/components/Progress";
export default {
components: {
Footer,
Progress,
},
};
</script>
<template>
<section>
<div class="columns is-mobile pt-6" v-show="progressBar()">
<div class="column is-three-fifths is-offset-one-fifth">
<progress
class="progress is-success"
:value="(progress.count / 9) * 100"
max="100"
>{{ (progress.count / 9) * 100 }} %</progress
>
</div>
</div>
</section>
</template>
<script>
import { mapGetters } from "vuex";
export default {
name: "Progress",
methods: {
progressBar() {
console.log(this.progress.status);
if (this.progress.status == false) {
return false;
}
return true;
},
},
computed: mapGetters({
progress: "getProgressBar",
}),
};
</script>
And vuex store for the progressbar.
src/store/modules/ProgressBar.js
const state = {
count: 1,
status: false,
};
const getters = {
getProgressBar: state => state,
};
const actions = {
};
const mutations = {
setProgressBar(state, value) {
state.status = value;
},
progressIncrease(state) {
state.status = state.status + 1;
console.log(state.status);
}
};
export default {
state,
getters,
actions,
mutations,
};
/basic;
<template>
/* code for form layout goes here*/
</template>
<script>
export default {
name: "Basics",
methods: {
toNetworks() {
this.$router.push({ name: "Location" });
this.$store.commit("progressIncrease");
},
/* other codes to handel input */
},
created(){
this.$store.commit("setProgressBar", true);
}
};
</script>
With the above code I'm trying to increment the variable count in the state as per the form step increases and show the progress bar according to the calculation.
The states are set correctly and I can increment it with the commit. But the progresses bar component is not reacting to the updated data in the state.
I know that mapGetter method is called only once when the <Progress/> component is loaded in the App.vue component.
Is there any method/way to make the <Progress/> react to changes on the data in the state that are made from router component ?
state.count = state.count + 1in here?progressIncrease(state) {state.status = state.status + 1;console.log(state.status);}- Tingsen Cheng