renoguyon / vuejs-noty

A Vue JS wrapper around Noty
156 stars 29 forks source link

Usage of $noty inside vuex #10

Closed ChunAllen closed 5 years ago

ChunAllen commented 5 years ago

How can I use the $noty inside Vuex modules?

Currently what I'm doing is this: pages/index.vue

export default { 
  created() {
    this.$store.dispatch('getUsers, this) // currently I need to pass this in order to use the $noty inside vuex module
  }, 
  computed: {
    users() {
      return this.$store.getters.users;
    }
  }
}
</script>

store/modules/users.js

export const state = () => ({
   users: []
});

export const actions = () => ({
  getUsers({commit}, context) {
      context.$noty.success('HELLO WORLD') `How can I use $noty without passing the this from the component?`
  } 
});
renoguyon commented 5 years ago

Hey, have you tried this syntax?

store/modules/users.js

import Vue from 'vue';

export const state = () => ({
   users: []
});

export const actions = () => ({
  getUsers({commit}) {
      Vue.noty.success('HELLO WORLD');
  } 
});

Let me know!

ChunAllen commented 5 years ago

It's working now. Thanks @renoguyon