Vue.js 如何在vue实例中调用(普通)websocket.onmessage方法

Vue.js 如何在vue实例中调用(普通)websocket.onmessage方法,vue.js,websocket,vuex,Vue.js,Websocket,Vuex,设置vue应用程序,使其具有来自vanilla websockets的消息功能,因此没有socket.io等。接收消息部分让我感到困惑,因为它不是在通常的情况下调用的,而是在服务器将消息推送到客户端之后。以下是我基本上想要做的,但在方法或计算属性中是不允许的 this.$store.state.SocketModule.webSocket.onmessage = function (msg) { this.$store.dispatch('SocketModule/onmessage', m

设置vue应用程序,使其具有来自vanilla websockets的消息功能,因此没有socket.io等。接收消息部分让我感到困惑,因为它不是在通常的情况下调用的,而是在服务器将消息推送到客户端之后。以下是我基本上想要做的,但在
方法
计算
属性中是不允许的

this.$store.state.SocketModule.webSocket.onmessage = function (msg) {
  this.$store.dispatch('SocketModule/onmessage', msg)
  // handle the msg in vuex
}
所以我应该做一些类似的事情

computed: {
  onmessage: this.$store.state.SocketModule.webSocket.onmessage = function (msg) 
    {
      this.$store.dispatch('SocketModule/onmessage', msg)
      // handle the msg in vuex
     }
}
这样行吗?(尚未完成套接字服务器)或类似的操作

<script>
   this.$store.state.SocketModule.webSocket.onmessage = function (msg) {
     this.$store.dispatch('SocketModule/onmessage', msg)
     // handle the msg in vuex
   }
export default {
   ...

这是。$store.state.SocketModule.webSocket.onmessage=function(msg){
此.store.dispatch('SocketModule/onmessage',msg)
//在vuex中处理消息
}
导出默认值{
...
服务器推送消息而不是从客户机获取消息与使用不同