在Angular 2中导入Socket.io客户端,获取依赖项错误

时间:2016-12-03 19:36:02

标签: javascript angular socket.io

我尝试使用方法How to import socket.io-client in a angular 2 application?

将Socket.io客户端添加到我的Angular 2应用程序中

但现在我遇到了Socket.io客户端JavaScript文件依赖项的错误。

image

以下是TypeScript文件

import { Component, OnInit,NgZone } from '@angular/core';
import { GroundService } from '../../services/ground.service';
import { Ground } from '../../../Ground';

import * as io from "socket.io-client";


@Component({
    moduleId: module.id,
    selector: 'ground',
    templateUrl: 'ground.component.1.html',
    styleUrls: ['ground.component.css']
})

export class GroundComponent{

    ground: Ground[];
    countttt : Number =0;
    constructor(private groundService:GroundService,private ngZone:NgZone){
        const socket = io.connect('http://localhost:30003');
        this.groundService.getGround()
        .subscribe(ground => {
            console.log(ground);
            this.ground = ground;        
        });
      socket.on("on:chat message", (data) => {
          console.log("aa");
    });
    }
}

我的输入.json

{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160725163759",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "node": "registry:dt/node#6.0.0+20160909174046",
    "socket.io-client": "registry:dt/socket.io-client#1.4.4+20160317120654"
  }
}

以下是我在上面的TypeScript文件中导入的Socket.io-client.js文件的前几行中的依赖项。所以基本上当它在TS文件中导入Socket.io客户端时,它无法找到文件中的错误。

/**
 * Module dependencies.
 */

var parser = require('socket.io-parser');
var Emitter = require('component-emitter');
var toArray = require('to-array');
var on = require('./on');
var bind = require('component-bind');
var debug = require('debug')('socket.io-client:socket');
var hasBin = require('has-binary');

下面是我要导入的Socket.io客户端文件夹中的节点模块文件夹

node module folder in the Socket.io client folder

以下是system.config.js

  System.config({
        map: {
             "socket.io-client": "node_modules/socket.io-client/lib/socket.js",
        },
        packages: {

             "socket.io-client": {"defaultExtension": "js"},
        }
)};

1 个答案:

答案 0 :(得分:0)

我传入了错误的网址。在socket.io-client的System.config中。 - Kshitij Mehta

相关问题