我尝试使用方法 How to import socket.io-client in a angular 2 application? 将 Socket.io 客户端添加到我的 Angular 2 应用程序中
但现在我遇到了 Socket.io 客户端 JavaScript 文件依赖项的错误。
下面是 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"},
}
)};
最佳答案
我传递的 URL 不正确。在socket.io-client 的System.config 中。 – Kshitij Mehta
关于javascript - 在 Angular 2 中导入 Socket.io 客户端,出现依赖项错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40951707/