我尝试将socket io客户端包含在一个角度2应用程序中,我收到了这个错误:
zone.js:1805 GET http://localhost:8080/node_modules/socket.io-client/socket.io.js 404 (未找到)(索引):26 ZoneAwareError {__zone_symbol__error:错误: (SystemJS)XHR错误(404 Not Found)加载 http://localhost:8080/node_modules/socket.io-cli ..., __zone_symbol__stack:"(SystemJS)XHR错误(404 Not Found)加载http:... 0 / app / protected / chat / chat.service / chat.service.js",originalErr: ZoneAwareError}
package.json
看起来像这样:
{
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@angular/common": "~2.3.0",
"@angular/compiler": "~2.3.0",
"@angular/core": "~2.3.0",
"@angular/forms": "~2.3.0",
"@angular/http": "~2.3.0",
"@angular/platform-browser": "~2.3.0",
"@angular/platform-browser-dynamic": "~2.3.0",
"@angular/router": "~3.3.0",
"@angular/upgrade": "~2.2.0",
"angular-in-memory-web-api": "~0.2.4",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"ng2-webstorage": "^1.5.0",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.1",
"socket.io-client": "^1.7.2",
"systemjs": "0.19.40",
"zone.js": "^0.7.6"
},
"devDependencies": {
"@types/core-js": "^0.9.34",
"@types/node": "^6.0.45",
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3"
},
"repository": {}
}
并且systemjs.config.js
看起来像这样:
(function(global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
// other libraries
"socket.io-client": 'npm:socket.io-client',
'rxjs': 'npm:rxjs'
},
// packages告诉System loader如何在没有文件名时加载 和/或没有扩展名
packages: {
'ng2-webstorage': { main: 'bundles/core.umd.js', defaultExtension: 'js' },
"socket.io-client": { main: './socket.io.js' },
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
我这样用过:
import * as io from "socket.io-client";
this.socket = io.connect("http://localhost:8080");
有什么想法吗?