Search code examples
javascriptangulartypescriptsystemjs

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


The following code gives

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')
  }

}

And the angular2-websocket code can be seen here: https://github.com/afrad/angular2-websocket/blob/master/src/angular2-websocket.ts

I've looked into Angular 2 Di not working - cannot resolve all parameters for and Angular 2 RC 4 "(SystemJS) Can't resolve all parameters for [object Location]: " in IE 11 but they don't seem to be the same problem as mine.

I can see that there's a problem with the parameters for the Websocket. It's not possible to call parameters in the constructor's parenthesis, and even if I take it off and leave only providers: [$WebSocket] I still get the error. If I leave only

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

there's no error

What exactly are those paremeters that SystemJS is trying to resolve?

My emitDecorator is true even though I don't know what this is for.


Solution

  • try this

    //...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{       
      // ...
    
    }
    

    the $WebSocket constructor require 1 param, you need provide it.

    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