1

while trying to get an updateable data in a tag I am only receiving the initial data, I can see the data updating in the debugger. and after changeing the state I can not display the new data:

I'm doing all the update thing in another component I only need to display the updated data from store

component.vue

<template lang="js">
    <div>
        <h1>hello {{ store.getters.myData}}</h1>

  <a><router-link to="/register">Go to Sign up</router-link></a>
  <a><router-link to="/">Go to Home</router-link></a>
  <router-view></router-view>
    </div>
</template>
<script>
import store from "../store";
export default {
};
</script>

store.js

import Vuex from 'vuex'

const state = { data: 'initial data'}
const getters = { myData(state){ return state.data; } }
const mutations = { UPDATE_DATA(state, data){ state.data = data; } }
const actions = { setData(data) { store.commit("UPDATE_DATA", data);} }

const store = new Vuex.Store({
  state,
  actions,
  mutations,
  getters
})

export default store
11
  • @EDG956 yeah sorry about that, I made an update and also checking your reference Commented Dec 21, 2021 at 9:13
  • I've tried this line but nothing was happend: <h1 :key="this.$store.state.data">hello {{ this.$store.state.data }}</h1> Commented Dec 21, 2021 at 9:15
  • I think you should have a look at this Commented Dec 21, 2021 at 9:26
  • @EDG956 take a look at my edit section Commented Dec 21, 2021 at 9:42
  • 1
    @yaronshamul Add computed property in your component and return this data - this.$store.getters.getData and use the computed property in template Commented Dec 21, 2021 at 10:31

1 Answer 1

2

You should use vuex mapGetters function to display your data.

In your component file import your getter inside the computed property

Import mapGetters with import {mapGetters} from "vuex";

computed: {
     ...mapGetters({
        myData: 'store/myData'
     })
}

Your data should be updated any times your store is updated

Sign up to request clarification or add additional context in comments.

6 Comments

can you please take a look on this link
ok I removed one of these mapGetters wrapers and it doesn't give me an error but still doesn't work as wanted: see this pastebin
Have a look a this code pen and tell me if it answer your question about how to connect with the store
can you please add some change so it will feat vue version 3.x?
Here is the app using version 3.0.0 : link
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.