【问题标题】:Global is not defined at ../node_modules/socket.io-parser/is-buffer.js全局未在 ../node_modules/socket.io-parser/is-buffer.js 中定义
【发布时间】:2018-10-26 16:33:54
【问题描述】:

提前感谢您帮助我。我正在尝试在我的一个角度组件中连接套接字,但在浏览器的控制台中它会抛出一个错误,指出全局未定义在 Object ../node_modules/socket.io-parser/is-buffer.js

这是我的 home.component.ts

import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import * as io from 'socket.io-client';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}


var socket = io();

如果我取出 socket.io-client 和 socket = io();该网站很好,但是当我包含它时,它崩溃了。

这是我的 package.json 文件

  "dependencies": {
    "@angular/animations": "^6.0.0",
    "@angular/common": "^6.0.0",
    "@angular/compiler": "^6.0.0",
    "@angular/core": "^6.0.0",
    "@angular/forms": "^6.0.0",
    "@angular/http": "^6.0.0",
    "@angular/platform-browser": "^6.0.0",
    "@angular/platform-browser-dynamic": "^6.0.0",
    "@angular/router": "^6.0.0",
    "@types/jquery": "^3.3.1",
    "@types/socket.io": "^1.4.33",
    "body-parser": "^1.18.3",
    "bootstrap": "^4.1.1",
    "core-js": "^2.5.4",
    "express": "^4.16.3",
    "jquery": "^3.3.1",
    "moment": "^2.22.1",
    "mongodb": "^3.1.0-beta4",
    "ng4-twitter-timeline": "^1.0.8",
    "popper.js": "^1.14.3",
    "rxjs": "^6.0.0",
    "socket.io": "^2.1.0",
    "socket.io-client": "^2.1.0",
    "zone.js": "^0.8.26"
  }

我第一次尝试只使用“socket.io”,但它不起作用。然后我开始研究类似的问题。我什么也没找到。

我的最终目标是使用套接字从表单中获取数据并存储在数据库中。任何帮助将不胜感激。

更新(可能的解决方案):2018 年 5 月 17 日 你必须使用:

(window as any) = window 

在 polyfill.ts 中,这为我解决了问题。

更新(可能的解决方案 #2):2018 年 5 月 29 日

(window as any).global = window

【问题讨论】:

标签: javascript node.js angular angular6


【解决方案1】:
(window as any).global = window

正如评论中已经提到的,将上述代码添加到polyfills.ts文件中。

【讨论】:

    【解决方案2】:

    在 src/index.html 在头部添加

    <script>
        var global = global || window;
    </script>
    

    【讨论】:

      【解决方案3】:

      要解决此问题,您需要打开文件your_angular_setup/src/polyfills.ts,然后在文件底部添加以下行。

          (window as any).global = window
      

      你会发现你的问题已经解决了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-15
        • 2017-12-23
        • 1970-01-01
        • 2022-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-27
        相关资源
        最近更新 更多