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

4,661 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第10天,点击查看活动详情

废话只说一句:码字不易求个👍,收藏 === 学会,快行动起来吧!🙇‍🙇‍🙇‍。

感谢点赞、收藏、关注和提出建议的小伙伴,希望大家工作顺利、老板能给你们加鸡腿!

问题

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

image.png

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 报错问题,但这种情况还会继续报其他错误,所以不建议使用。

image.png

解决方法 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 的过程中,总是不经意的会遇到一些问题,耐心的去寻找答案,去分析解决,总是会有办法的。