如何在 Angular 2 中的服务中注入服务时传递依赖参数(Ionic 2 / Angular 2 / Typescript)

2024-01-03

我正在制作一个示例应用程序,以在打字稿中连接到 ionic 2 中的 websocket 服务器。链接到repo https://github.com/mehdhi/ionic2websocketconnectionsample

我的要求是在应用程序启动期间建立 websocket 连接

我在用着angular2-websocket https://github.com/afrad/angular2-websocket创建连接。

参考 :

  1. http://blog.thoughtram.io/angular/2015/09/17/resolve-service-dependency-in-angular-2.html http://blog.thoughtram.io/angular/2015/09/17/resolve-service-dependencies-in-angular-2.html

  2. http://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html http://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html

我收到错误“无法解析 '$WebSocket'(String, Array, ?) 的所有参数。请确保所有参数均使用 Inject 修饰或具有有效的类型注释,并且 '$WebSocket' 均使用 Injectable 修饰。 ”

CODE: app.ts https://github.com/mehdhi/ionic2websocketconnectionsample/blob/master/app/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]);

连接服务.ts https://github.com/mehdhi/ionic2websocketconnectionsample/blob/master/app/framework/connection/connection-service.ts

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() 注释的(特定于离子)提供程序字段而不是使用引导程序

bootstrap(ConnectionService, 
    [provide($WebSocket, useValue: new WebSocket("ws://echo.websocket.org")]);

Eg.

@App({
  template: '<ion-nav [root]="rootPage"></ion-nav>',
  config: {},
  providers : [ provide( $WebSocket, { useValue: new $WebSocket("ws://echo.websocket.org") }), ConnectionService]
})

可以找到工作样本here https://github.com/mehdhi/ionic2websocketconnectionsample

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Angular 2 中的服务中注入服务时传递依赖参数(Ionic 2 / Angular 2 / Typescript) 的相关文章