在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解决方案

Original: https://www.cnblogs.com/aWxvdmVseXc0/p/14426317.html
Author: windy_ll
Title: 在vue中使用websocket回调函数中调用其他methods函数报错Uncaught TypeError: this.getHistory is not a function at WebSocket.ws.onmess解决方案

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/686211/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球