加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 10:17:50 所属栏目:安全 来源:网络整理
导读:我使用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 er
我使用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

角-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)'
    ]
  });
};

系统config.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中逐个添加它们来包含丢失的lib来消除一些错误.但我怀疑这是正确的做法.

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',...
};
我找到了解决这个问题的方法.

问题是:npm install sockjs-client下载sockjs-node而不是client.我希望他们能尽快解决.

在你的项目中,你指向vendor / sockjs-client / lib / entry.js而不是你应该下载这个文件http://cdn.jsdelivr.net/sockjs/1/sockjs.min.js把它放在你的项目中在system-config.ts中指向它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读