在vue中使用websocket回调函数中调用其他methods函数报错Uncaught TypeError: this.getHistory is not a function at WebSocket.ws.onmess解决方案

1、报错详情

????在vue中,使用了websocket类,在websocket的onmessage回调函数中调用methods的getHistory函数,发现一调用就报错Uncaught TypeError: this.getHistory is not a function at WebSocket.ws.onmess,报错截图如下:

在vue中使用websocket回调函数中调用其他methods函数报错Uncaught TypeError: this.getHistory is not a function at WebSocket.ws.onmess解决方案


2、解决方案

????百度使用this.$options.methods来调用即可解决,尝试调用发现仍然报错,报错信息为Uncaught TypeError: Cannot read property ‘methods‘ of undefined at WebSocket;经过多次尝试,解决方案如下:将methods要被调用的方法通过window变量暴露给外部js,然后在onmessage函数中调用外部js函数即可,运行成功截图如下:

在vue中使用websocket回调函数中调用其他methods函数报错Uncaught TypeError: this.getHistory is not a function at WebSocket.ws.onmess解决方案

在vue中使用websocket回调函数中调用其他methods函数报错Uncaught TypeError: this.getHistory is not a function at WebSocket.ws.onmess解决方案

上一篇:在做微信分享到朋友圈时,手机扫码报config:invalid signature,分享后后正常的问题,是url问题


下一篇:微信视频直播测试