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

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

源码附件已经打包好上传到百度云了,大家自行下载即可~

链接: https://pan.baidu.com/s/14G-bpVthImHD4eosZUNSFA?pwd=yu27
提取码: yu27
百度云链接不稳定,随时可能会失效,大家抓紧保存哈。

如果百度云链接失效了的话,请留言告诉我,我看到后会及时更新~

开源地址
码云地址:
http://github.crmeb.net/u/defu

Github 地址:
http://github.crmeb.net/u/defu

猜你喜欢

转载自blog.csdn.net/qq_39221436/article/details/125215467
今日推荐