全局未在../node_modules/socket.io-parser/is-buffer.js中定义

Dre*_*son 12 javascript node.js angular angular6

在此先感谢帮助我.我正在尝试在我的一个角度组件中连接套接字,但是在浏览器的控制台中它会抛出一个错误,说明没有在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();
Run Code Online (Sandbox Code Playgroud)

如果我拿出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"
  }
Run Code Online (Sandbox Code Playgroud)

我第一次尝试只是'socket.io',但没有用.然后我开始研究类似的问题.我一无所获.

我的最终目标是使用socket从表单中获取数据并存储在数据库中.任何帮助将非常感激.

更新(可能的解决方案):05/17/2018 您必须使用:

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

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

更新(可能的解决方案#2):05/29/2018

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

Ric*_*rma 10

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

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