vuex get data in mounted

Solutions on MaxInterview for vuex get data in mounted by the best coders in the world

showing results for - "vuex get data in mounted"
Théodore
18 Jul 2020
1const store = new Vuex.Store({
2  state: {
3    globalCompanies: {
4      test: null
5    }
6  },
7  mutations: {
8    setMe: (state, payload) => {
9      state.globalCompanies.test = payload
10    }
11  },
12  actions: {
13    pretendFetch: ({commit}) => {
14      setTimeout(() => {
15        commit('setMe', 'My text is here!')
16      }, 300)
17    }
18  }
19})
20
21new Vue({
22  el: '#app',
23  store,
24  computed: {
25    cp: function() { // computed property will be updated when async call resolves
26      return this.$store.state.globalCompanies.test;
27    }
28  },
29  watch: { // watch changes here
30    cp: function(newValue, oldValue) {
31      // apply your logic here, e.g. invoke your listener function
32      console.log('was: ', oldValue, ' now: ', newValue)
33    }
34  },
35  mounted() {
36    this.$store.dispatch('pretendFetch');
37    // console.log(this.cp, this.$store.state.globalCompanies.test); // null
38    // var cn = this.$store.state.globalCompanies.test; // null
39    // console.log(cn) // null
40  }
41})