Angular 6 - browser-crypto.js:3 Uncaught ReferenceError: global is not defined [英] Angular 6 - browser-crypto.js:3 Uncaught ReferenceError: global is not defined
本文介绍了Angular 6 - browser-crypto.js:3 Uncaught ReferenceError: global is not defined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在实现 socketjs.但是我遇到了以下错误.
I am implementing socketjs. however i have encountered below error.
下面是我正在使用的 socket 和 stomp 包.
Below are the socket and stomp packages which i am using.
import * as SockJS from 'sockjs-client';
import * as Stomp from 'stompjs/lib/stomp.js';
提前致谢.
这是我的角度代码-
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();
}
});
});
}
推荐答案
在你的 polyfills.ts 中添加这个
Add this in your polyfills.ts
(window as any).global = window
首先你需要输入安装socket-client
First you need to install socket-client with typing
npm install --save @types/socket.io
然后你可以像这样在你的组件或服务中导入 socket.io-client
Then you can import the socket.io-client in your component or service like this
import * as Socket from 'socket.io-client';
像这样改变你的代码
let ws = Socket(this.serverUrl);
这篇关于Angular 6 - browser-crypto.js:3 Uncaught ReferenceError: global is not defined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文