se-panfilov / vue-notifications

Vue.js agnostic library for non-blocking notifications
https://se-panfilov.github.io/vue-notifications/
MIT License
698 stars 54 forks source link

How to emit notification from vuex mutation? #153

Open bashlakov opened 5 years ago

bashlakov commented 5 years ago

Hi! Is there a way to emit notification from vuex mutation? I'm trying to show notification on websocket message received event. I'm using vue-native-websocket and nuxt, so my store's index.js looks like that:

import Vuex from 'vuex'

const createStore = () => {
  return new Vuex.Store({
    state : {
      socket: {
        isConnected: false,
      }
    },
    mutations: {
      SOCKET_ONOPEN (state, event)  {
        state.socket.isConnected = true
      },
      SOCKET_ONCLOSE (state, event)  {
        state.socket.isConnected = false
      },
      SOCKET_ONMESSAGE (state, message)  {
        console.log(message);
      },
    },
    notifications: {
      showSomething: {
        title: 'Something happened',
        message: 'See console for details',
        type: 'error'
      }
    }
  })
}

export default createStore

How can I fire notification from SOCKET_ONMESSAGE mutation?

rajashekarappala commented 5 years ago

Hey @bashlakov, Have you found any solution?

RIO-LI commented 4 years ago

just like this?

import Vuex from 'vuex'
import Vue from 'vue'

const createStore = () => {
  return new Vuex.Store({
    state : {
      socket: {
        isConnected: false,
      },
      bus: new Vue({
              notifications: {
      showSomething: {
        title: 'Something happened',
        message: 'See console for details',
        type: 'error'
      }
    }
     })
    },
    mutations: {
      SOCKET_ONOPEN (state, event)  {
        state.socket.isConnected = true
      },
      SOCKET_ONCLOSE (state, event)  {
        state.socket.isConnected = false
      },
      SOCKET_ONMESSAGE (state, message)  {
        console.log(message);
       this.state.bus. showSomething();
      },
    }
  })
}

export default createStore