javascript - 错误 : (SystemJS) Can't resolve all parameters for $WebSocket: ([object Object], [对象对象],?)

标签 javascript angular typescript systemjs

以下代码给出

Error: (SystemJS) Can't resolve all parameters for $WebSocket: ([object Object], [object Object], ?).

app.component.ts

import { Component } from '@angular/core';
import {$WebSocket} from 'angular2-websocket/angular2-websocket'
import {OnInit} from '@angular/core';
import {Inject} from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: `app/root-template.html`,
  providers: [$WebSocket]
})
export class AppComponent implements OnInit{       
  constructor(private websocket:$WebSocket) {

  }

    ngOnInit(): void {

    console.log('init')
  }

}

Angular2-websocket 代码可以在这里看到:https://github.com/afrad/angular2-websocket/blob/master/src/angular2-websocket.ts

我调查过Angular 2 Di not working - cannot resolve all parameters forAngular 2 RC 4 "(SystemJS) Can't resolve all parameters for [object Location]: " in IE 11但他们似乎和我的问题不是同一个问题。

我可以看到 Websocket 的参数有问题。无法调用构造函数括号中的参数,即使我将其去掉并仅保留 providers: [$WebSocket] 我仍然收到错误。如果我只离开

import {$WebSocket} from 'angular2-websocket/angular2-websocket'

没有错误

SystemJS 试图解析的参数到底是什么?

我的emitDecorator是真的,尽管我不知道这是做什么的。

最佳答案

试试这个

//...import

let myws = new $WebSocket("ws://127.0.0.1:7000"); // config your url

@Component({
  //...
  providers: [{provide: $WebSocket, useValue: myws}]
})
export class AppComponent implements OnInit{       
  // ...

}

$WebSocket 构造函数需要 1 个参数,您需要提供它。

constructor(private url: string, private protocols?: Array<string>, private config?: WebSocketConfig, private binaryType?: BinaryType)

https://github.com/afrad/angular2-websocket/blob/master/src/angular2-websocket.ts#L45

关于javascript - 错误 : (SystemJS) Can't resolve all parameters for $WebSocket: ([object Object], [对象对象],?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42129669/

相关文章:

javascript - Typescript 可为 null 的回调

javascript - 如何隐藏网站的javascript文件?

javascript - 来自 JSON 数组的动态菜单

javascript - 我如何嵌套 2 个 promise 并让 ionViewCanLeave 等待结果?

typescript - 如何在 typescript 中编写多行函数定义?

angular - 在 Typescript/Angular2 组件中使用 MathJax

javascript - 格式化并验证文本区域中的多个电子邮件地址

javascript - 在动态加载的 es 模块中使用 React

css - 在 Docker 中使用自动构建无法使用 Bootstrap 进行样式化?

angular - 在 webcomponent 中重置 Angular 路由器状态( Angular 元素)