Vuex & Websockets

后端 未结 1 1277
孤独总比滥情好
孤独总比滥情好 2020-12-30 09:20

So currently I am working with VueJS 2 and I am very new with it. Now I was getting some help with some other people, but I am still stuck.

Here is what I want to ac

相关标签:
1条回答
  • 2020-12-30 09:45

    I have an electron application that uses Vue and a websocket for information and here is how I set mine up.

    I have a store defined that also actually creates and sets up the websocket.

    Store.js

    const socket = require("socket-library") // Take your pick of socket libs
    
    const mySocket = new socket(...)
    mySocket.on("message", message => store.handleMessage(message))
    ...other handlers...
    
    const store = {
        handleMessage(message){
            // do things with the message
        }
    }
    
    export default store
    

    Renderer.js

    import store from "./store"
    
    new Vue({
        data:{
            store
        }
    })
    

    This exposes my store at the root level of my Vue and allows me to pass data to components, or what have you. The store manages all the incoming information from the websocket.

    With you wanting to use Vuex, you could do essentially the same thing, where Vuex would be your store and when messages come in over the socket, you just pass them to Vuex.

    mySocket.on("message", msg => vuexStore.dispatch("onSocketMessage", msg))
    

    and set up your Vue and components to work with Vuex as you typically would.

    0 讨论(0)
提交回复
热议问题