Angular 6-browser-crypto.js:3 Uncaught ReferenceError:未定义全局

Uni*_*orn 4 spring-boot angular angular6

我正在实现socketjs。但是我遇到了以下错误。

Angular 6套接字错误 以下是我正在使用的套接字和脚踏软件包。

import * as SockJS from 'sockjs-client';
import * as Stomp from 'stompjs/lib/stomp.js';
Run Code Online (Sandbox Code Playgroud)

提前致谢。

这是我的角度代码-

import * as Socket from 'socket.io-client';
import * as Stomp from 'stompjs/lib/stomp.js';

initializeWebSocketConnection2(){

let ws = new Socket(this.serverUrl);

this.stompClient = Stomp.over(ws);
let that = this;
this.stompClient.connect({}, function(frame) {


   that.stompClient.subscribe("/test", function(message){ 
        if(message.body) {
          console.log(message.body);
         window.location.reload(); 
        }
 });


   that.stompClient.subscribe("/operation", function(message){ 
        if(message.body) {
          console.log(message.body);
          window.location.reload();

        }
 });
Run Code Online (Sandbox Code Playgroud)

});

}

Che*_*pan 8

将此添加到您的polyfills.ts中

(window as any).global = window
Run Code Online (Sandbox Code Playgroud)

首先,您需要安装带有类型的套接字客户端

npm install --save @types/socket.io
Run Code Online (Sandbox Code Playgroud)

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

import * as Socket from 'socket.io-client';
Run Code Online (Sandbox Code Playgroud)

像这样更改代码

let ws = Socket(this.serverUrl);
Run Code Online (Sandbox Code Playgroud)

  • polyfills.ts 中的 `window as any).global = window;` 解决了我的问题 https://github.com/angular/angular-cli/issues/8160#issuecomment-386153833 (4认同)
  • (任意窗口).global = window; 将其添加到您的代码中并立即检查 (3认同)
  • (任意窗口).global = window; 在我的 polyfill.ts 的底部也修复了我遇到的这个问题,谢谢! (2认同)

V.V*_*gar 7

在index.html的头部添加以下代码段解决了我的问题 -

<script type="application/javascript">
  var global = window;
</script>
Run Code Online (Sandbox Code Playgroud)

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