in Education by
I am developing a single-page-application using vue-cli3 and npm. The problem: Populating a basic integer value (stored in a vuex state) named counter which was incremented/decremented in the backend to the frontend, which displays the new value. The increment/decrement mutations are working fine on both components (Frontend/Backend), but it seems like the mutations don't work on the same route instance: When incrementing/ decrementing the counter in backend, the value is not updated in the frontend and otherwise. store.js: Contains the state which needs to be synced between Backend/Frontend. import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { counter: 10 }, mutations: { increment (state) { state.counter++ }, decrement (state) { state.counter-- } } }) index.js: Defines the routes that the vue-router has to provide. import Vue from 'vue' import Router from 'vue-router' import Frontend from '@/components/Frontend' import Backend from '@/components/Backend' Vue.use(Router) export default new Router({ routes: [ { path: '/', name: 'Frontend', component: Frontend }, { path: '/backend', name: 'Backend', component: Backend } ], mode: 'history' }) main.js: Inits the Vue instance and provides the global store and router instances. import Vue from 'vue' import App from './App' import router from './router' import { sync } from 'vuex-router-sync' import store from './store/store' Vue.config.productionTip = false sync(store, router) new Vue({ router, store, render: h => h(App) }).$mount('#app') Frontend.vue/Backend.vue: Both (Frontend/Backend) use the same code here. They use the state counter in order to display and modify it.
Counter: {{ getCounter }}

export default { name: 'Frontend', methods: { increment () { this.$store.commit('increment') }, decrement () { this.$store.commit('decrement') } }, computed: { getCounter () { return this.$store.state.counter } } } It would be awesome if someone sould tell me what I am missing or if I have misunderstood the concept of vuex and vue-router. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Just get the counter from the store for both components. You don't need data as store is already reactive.
Counter: {{ counter }}

import { mapState, mapMutations } from 'vuex'; export default { name: 'Frontend', methods: { ...mapMutations([ 'increment', 'decrement', ]) }, computed: { ...mapState({ counter: state => state.counter, }) } } For reference: mapState: https://vuex.vuejs.org/guide/state.html#the-mapstate-helper mapMutations: https://vuex.vuejs.org/guide/mutations.html#committing-mutations-in-components

Related questions

0 votes
    I am developing a single-page-application using vue-cli3 and npm. The problem: Populating a basic integer ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have two functions in my store, one that gets data by calling API and one that toggles change ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    I have set up Vue so that I have a top level AppLayout component which just includes a Navigation Menu ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    recently I started to work on Vue js and I want to use a javascript plugin like Isotope also I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    Tell me how to make it so that when you click on a button from a cycle with page numbers, this ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm trying to get a modal to pop up on cellClick. I'm not getting any errors, but it's not ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    Basically I need to add the script to the head of my index.html, so what I've tried is... in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    Basically I need to add the script to the head of my index.html, so what I've tried is... in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: When to use ":"(colon) operator in javascript vs "=" operator ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: When to use ":"(colon) operator in javascript vs "=" operator ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    What is the difference between hub,switch and router?In tabular form? Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    Differentiate between Router and Filter Transformation?...
asked Mar 28, 2021 by JackTerrance
0 votes
    What are the similarities and differences between ROUTER and FILTER?...
asked Mar 26, 2021 in Technology by JackTerrance
0 votes
    What is the difference between Hub, Switch, and Router?...
asked Nov 20, 2020 in Education by Editorial Staff
0 votes
    i'm messing with React and i was trying to make some transition between the router pages. Before using ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
...