L
L
LionG2019-10-11 06:11:30
Vue.js
LionG, 2019-10-11 06:11:30

Why doesn't reactivity (VUEX) work?

I'm trying to write a notification module for VUEjs using a repository.
notify.js

export default {
    state: {
        Notifyes: [
            {title:'Default notify', desc:'some desc', type:'info', timeout:false}
        ]
    },
    getters: {
        Notifyes(state) {
            console.log('getter Notifyes');
            return state.Notifyes;
        }
    },
    mutations: {
        addNotify(state, notify = {title:'Empty notify', desc:'', type:'error', timeout:false}) {
            //types: danger warning success info primary secondary light
            console.log('mutation addNotify');
            state.Notifyes.push(notify);
        }
    },
    actions: {
        addNotify(ctx, notify) {
            console.log('action addNotify ');
            ctx.commit('addNotify', notify);
        }
    }
}

Notifyes.vue
<template>
    <div class="notifyes">
      <notify v-for="(Notify, id) in Notifyes"
              v-bind:key="id"
              v-bind:title="Notify.title"
              v-bind:desc="Notify.desc"
              v-bind:type="Notify.type"
      >
      </notify>
    </div>
</template>

<script>
  import Notify from './components/Notify.vue'
  import {mapGetters} from "vuex";

  export default {
    name: 'notifyes',
    computed: {
      ...mapGetters(['Notifyes'])
    },
    mounted() {
      console.log('mounted');
    },
    updated() {
      console.log('updated');
    },
    components: {
      Notify
    }
  }
</script>

In one of the parts of the application, I imported and called addNotify({title:'Hello world', desc:'And welcome to plugin!', type:'success', timeout:false});
Log:
getter Notifyes
mounted
action addNotify 
mutation addNotify
action addNotify 
mutation addNotify
action addNotify 
mutation addNotify
action addNotify 
mutation addNotify

"default notification" is displayed, but further changes do not cause any events. In theory, when adding, the order should be like this:
action addNotify 
mutation addNotify
updated

Why is updated not called because Notifyes is updated?
Tried declaring Notifyes in (data, computed, methods) using (mapState and mapGetters). Reactivity doesn't work. If you make changes to the code and save it, then apparently hot swapping works and mounted and then updated get into the console once.
UPD: I was dripping in the structure and found an interesting way. Apparently due to the fact that I moved the notify storage to a separate file - its path has changed a bit. If you use the code below, then reactivity starts working:
data: function() {
    return {
        Notifyes: this.$store.state.Notify.Notifyes
    }
},

Essentially mapState([Notifyes]) == (Notifyes = this.$store.state.Notifyes). And mapState([Notifyes]) would work if the notification store were global.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question