Sto creando un'applicazione di esempio per effettuare la connessione a un server Websocket in ionic 2 in dattiloscritto. link alla repoCome passare i parametri per la dipendenza durante l'iniezione di servizi nei servizi in Angular 2 (Ionic 2/Angular 2/Typescript)
La mia esigenza è quella di rendere il collegamento websocket durante l'applicazione si avvia
sto usando angular2-websocket per la creazione della connessione.
Riferimenti:
http://blog.thoughtram.io/angular/2015/09/17/resolve-service-dependencies-in-angular-2.html
http://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html
sto ottenendo un errore "non può risolvere tutti i parametri per '$ WebSocket' (? String, Array,) Assicurarsi che tutti i parametri siano decorati con Inject o abbiano annotazioni di tipo valide e che '$ WebSocket' sia decorato con Injectable. "
CODICE: app.ts
import {App, Platform} from 'ionic-framework/ionic';
import {TabsPage} from './pages/tabs/tabs';
import {ConnectionService} from './framework/connection/connection-service'
import {$WebSocket} from 'angular2-websocket/angular2-websocket';
import {bootstrap} from 'angular2/platform/browser';
// https://angular.io/docs/ts/latest/api/core/Type-interface.html
import {Type} from 'angular2/core';
@App({
template: '<ion-nav [root]="rootPage"></ion-nav>',
config: {}
})
export class MyApp {
rootPage: Type = TabsPage;
constructor(platform: Platform, private conn : ConnectionService) {
platform.ready().then(() => {
this.conn.connect();
});
}
}
bootstrap(MyApp, [$WebSocket, ConnectionService]);
import {Injectable, Component, Inject} from 'angular2/core';
import {$WebSocket} from 'angular2-websocket/angular2-websocket';
import {bootstrap} from 'angular2/platform/browser';
@Injectable()
export class ConnectionService {
private _status: number;
//private connection: $WebSocket;
constructor(private connection : $WebSocket = new $WebSocket("ws://echo.websocket.org")) {
console.log("Starting connection");
//this.connection = new $WebSocket("ws://echo.websocket.org");
this.connection.onClose(this.onCloseHandler);
this.connection.onError(this.onErrorHandler);
this.connection.onOpen(this.onOpenHandler);
this.connection.onMessage(this.onRecieveHandler, {});
}
...
public connect() {
this.connection.connect(true);
}
...
}
bootstrap(ConnectionService, [$WebSocket]);
'@App()' è un'annotazione specifica ionica? –
@Gunter sì è specifica per Ionic. Può essere importata da 'ionic-framework/ionic'; – tymspy
Ciò significa che non è necessario eseguire il bootstrap nelle app Ionic2 @tymSpy? –