Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue-mqtt in combination with the vuex #15

Open
cgHome opened this issue Jan 20, 2020 · 1 comment
Open

vue-mqtt in combination with the vuex #15

cgHome opened this issue Jan 20, 2020 · 1 comment

Comments

@cgHome
Copy link

cgHome commented Jan 20, 2020

Is there an example how to use the vue-mqtt in combination with the vuex ?

@vadimmelnicuk
Copy link

vadimmelnicuk commented Feb 19, 2020

In the App.vue in the mounted() block you can subscribe to mqtt topics.

mounted() {
      this.$mqtt.subscribe('topic/#')
  }

Then also in the App.vue in the mqtt block you can update certain veux state values using a comit, for example:

 mqtt: {
    'topic/#'(data, topic) {
      this.$store.commit('updateState', data)
    }
  }

Your store would look something like that:

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

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
     topicValue: null
  },
  mutations: {
    updateState(state, payload) {
      state.topicValue = payload
    }
  }
})

Finally, in any component you can map the store state:

import { mapState } from 'vuex'

export default {
  computed: {
    ...mapState(['topicValue'])
  }
}

I hope, this answers your question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants