Spring Boot Angular 6 - browser-crypto.js:3 Uncaught ReferenceError:global is not defined

f0brbegy  于 2024-01-06  发布在  Spring
关注(0)|答案(4)|浏览(218)

我正在实现socketjs.然而,我遇到了下面的错误.
x1c 0d1x下面是我正在使用的socket和stomp包。

  1. import * as SockJS from 'sockjs-client';
  2. import * as Stomp from 'stompjs/lib/stomp.js';

字符串
提前致谢。
下面是我的Angular代码-

  1. import * as Socket from 'socket.io-client';
  2. import * as Stomp from 'stompjs/lib/stomp.js';
  3. initializeWebSocketConnection2(){
  4. let ws = new Socket(this.serverUrl);
  5. this.stompClient = Stomp.over(ws);
  6. let that = this;
  7. this.stompClient.connect({}, function(frame) {
  8. that.stompClient.subscribe("/test", function(message){
  9. if(message.body) {
  10. console.log(message.body);
  11. window.location.reload();
  12. }
  13. });
  14. that.stompClient.subscribe("/operation", function(message){
  15. if(message.body) {
  16. console.log(message.body);
  17. window.location.reload();
  18. }
  19. });


});
}

avwztpqn

avwztpqn1#

将此添加到polyfills.ts中

  1. (window as any).global = window

字符串
首先,你需要安装socket-client,

  1. npm install --save @types/socket.io


然后,您可以像这样在组件或服务中导入socket.io-client

  1. import * as Socket from 'socket.io-client';


像这样更改代码

  1. let ws = Socket(this.serverUrl);

展开查看全部
ujv3wf0j

ujv3wf0j2#

在index.html的 head 中添加以下片段解决了我的问题-

  1. <script type="application/javascript">
  2. var global = window;
  3. </script>

字符串
参考https://github.com/stomp-js/ng2-stompjs/issues/70

v7pvogib

v7pvogib3#

添加以下代码对我来说很有用

  1. <script>
  2. var global = window; // fix global is undefined in socketjs-client
  3. </script>

字符串

i1icjdpr

i1icjdpr4#

我解决这个问题使用内置版本

  1. import sockjs from "sockjs-client/dist/sockjs"

字符串

相关问题