typescript - 如何将 SockJS 添加到 Angular 2 项目中?

标签 typescript angular systemjs angular-cli angular2-cli

我使用 angular-cli 生成一个新项目。现在我想在项目中添加 socketJs,但是浏览器控制台一直报错:

GET http://localhost:4200/url-parse 404 (Not Found)
GET http://localhost:4200/inherits 404 (Not Found)
Unhandled Promise rejection: Error: XHR error (404 Not Found) loading http://localhost:4200/url-parse
        at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:4200/vendor/zone.js/dist/zone.js:769:30)
        at ZoneDelegate.invokeTask (http://localhost:4200/vendor/zone.js/dist/zone.js:356:38)
        at Zone.runTask (http://localhost:4200/vendor/zone.js/dist/zone.js:256:48)
        at XMLHttpRequest.ZoneTask.invoke (http://localhost:4200/vendor/zone.js/dist/zone.js:423:34)
    Error loading http://localhost:4200/url-parse as "url-parse" from http://localhost:4200/vendor/sockjs-client/lib/main.js ; Zone: <root> ; Task: Promise.then ; Value: Error: Error: XHR error (404 Not Found) loading http://localhost:4200/url-parse(…)
Error: Uncaught (in promise): Error: Error: XHR error (404 Not Found) loading http://localhost:4200/url-parse(…)
GET http://localhost:4200/json3 404 (Not Found)
GET http://localhost:4200/debug 404 (Not Found)
GET http://localhost:4200/events 404 (Not Found)
GET http://localhost:4200/eventsource 404 (Not Found)
GET http://localhost:4200/crypto 404 (Not Found)
GET http://localhost:4200/json3 404 (Not Found)
GET http://localhost:4200/url-parse 404 (Not Found)
GET http://localhost:4200/debug 404 (Not Found)
GET http://localhost:4200/inherits 404 (Not Found)
Assertion failed: loading or loaded
GET http://localhost:4200/events 404 (Not Found)
Assertion failed: loading or loaded
GET http://localhost:4200/faye-websocket 404 (Not Found)
Assertion failed: loading or loaded
GET http://localhost:4200/eventsource 404 (Not Found)
Assertion failed: loading or loaded
GET http://localhost:4200/http 404 (Not Found)
GET http://localhost:4200/https 404 (Not Found)

这是我的步骤和配置:

typings install sockjs-client --save --ambient

Angular -cli-build.js

var Angular2App = require('angular-cli/lib/broccoli/angular2-app');

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'systemjs/dist/system-polyfills.js',
      'systemjs/dist/system.src.js',
      'zone.js/dist/**/*.+(js|js.map)',
      'es6-shim/es6-shim.js',
      'reflect-metadata/**/*.+(ts|js|js.map)',
      'rxjs/**/*.+(js|js.map)',
      '@angular/**/*.+(js|js.map)',
      'sockjs-client/**/*.+(js)'
    ]
  });
};

系统配置.ts

/** Map relative paths to URLs. */
const map: any = {
  'sockjs-client': 'vendor/sockjs-client/lib/entry.js'
};

/** User packages configuration. */
const packages: any = {
  'vendor/sockjs-client/lib': {
    'format': 'cjs',
    'defaultExtension': 'js'
  }
};

我的自定义服务类:

import { Injectable } from '@angular/core';

import * as SockJS from 'sockjs-client';
import BaseEvent = __SockJSClient.BaseEvent;
import SockJSClass = __SockJSClient.SockJSClass;

@Injectable()
export class WebsocketService {

  private sockJs: SockJSClass;

  constructor() {
    console.log('constuctor');
    this.sockJs = new SockJS('/hello');
  }
}

请告诉我如何解决它,非常感谢!

但是,我可以通过在 system-config.ts 中一一添加缺少的库来消除一些错误。但我怀疑这是正确的做法。

const map: any = {
  'sockjs-client': 'vendor/sockjs-client/lib/entry.js',
  'json3': 'vendor/sockjs-client/node_modules/json3/lib/json3.js',
  'url-parse': 'vendor/sockjs-client/node_modules/url-parse/index.js',
  'inherits': 'vendor/sockjs-client/node_modules/inherits/inherits.js',
  'debug': 'vendor/sockjs-client/node_modules/debug/debug.js',
  ...
};

最佳答案

在新版本的 angular2 CLI 中 https://cli.angular.io/添加库很简单。

如果只想添加sockjs_client

1) npm i --save sockjs-client

2) 在 typings.d.ts 中添加此声明 declare module 'sockjs-client';

但我会推荐对 Angular 2 使用 STOMP-Over-WebSocket 服务

安装这个 npm 包

npm i --save stompjs
npm i --save sockjs-client
npm i --save ng2-stomp-service

typings.d.ts

添加stompjssockjs-client模块声明

declare module 'stompjs';
declare module 'sockjs-client';

app.module.ts

import { StompService } from 'ng2-stomp-service';

@NgModule({
  ...
  providers: [StompService]
})

app.components.ts

import { StompService } from 'ng2-stomp-service';

private wsConf = {
  host:'test.com'
}

constructor(stomp: StompService) {

  stomp.configure(this.wsConf);

  stomp.startConnect().then(() => {
    console.log('connected');
  });


}

来源https://github.com/devsullo/ng2-STOMP-Over-WebSocket

关于typescript - 如何将 SockJS 添加到 Angular 2 项目中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38278853/

相关文章:

angularjs - Angular ,页面刷新时数据丢失

angular - 多个 ngx 分页错误 Angular 2

jspm - 如何填充依赖于全局 jQuery 和 lodash 的非 CommonJS、非 AMD 包?

angular - 在 Angular 2 中使用 SystemJS 和 Karma 加载节点模块

TypeScript 系统模块未注册导入的文件位置

javascript - 如何在下拉列表中显示实际值并可能设置此列表中的其他选项

f# - 实现 F# 库以供 TypeScript/Javascript 使用?

javascript - 继续使用代码 1 退出 "tsc.exe"

angular - 您如何使用 Angular 6 和不使用 jasmine-marbles 对 ngrx 效果进行单元测试?

ajax - 在不执行多个请求的情况下多次订阅 rxjs Observer