解决vue3引入sockjs-client报错问题(global is not defined)

米阳 2022-9-10 246 9/10

今天在 vue3 中引入 sockjs-client 的时候莫名的报了个错,而且页面里也没有 global 相关的内容,使得 sockjs-client 无法使用。报错信息如下:

Uncaught ReferenceError: global is not defined
    at node_modules/sockjs-client/lib/utils/event.js (event.js:8:27)
    at __require2 (chunk-A5AMJUWA.js?v=0a8d1f98:15:44)
    at node_modules/sockjs-client/lib/transport/websocket.js (websocket.js:3:13)
    at __require2 (chunk-A5AMJUWA.js?v=0a8d1f98:15:44)
    at node_modules/sockjs-client/lib/transport-list.js (transport-list.js:5:3)
    at __require2 (chunk-A5AMJUWA.js?v=0a8d1f98:15:44)
    at node_modules/sockjs-client/lib/entry.js (entry.js:3:21)
    at __require2 (chunk-A5AMJUWA.js?v=0a8d1f98:15:44)
    at dep:sockjs-client:1:16

解决方法 1

还有种方法,gitHub解决办法讨论地址:github.com/sockjs/sock…

index.html 中, 添加 <script>global = globalThis</script>

虽然此时解决了 global 报错问题,但这种情况还会继续报其他错误,所以不建议使用。

解决方法 2

改变引入方式,将 import SockJS from 'sockjs-client'; 改为 import SockJS from 'sockjs-client/dist/sockjs.min.js';

//import SockJS from  'sockjs-client';
import SockJS from  'sockjs-client/dist/sockjs.min.js';
import Stomp from "stompjs";

在过渡到 vue 3 的过程中,总是不经意的会遇到一些问题,慢慢来,总是会有办法的。

- THE END -

米阳

10月10日17:36

最后修改:2024年10月10日
0