【问题标题】:How to pass parameters for dependency while Injecting services in services in Angular 2 ( Ionic 2 / Angular 2 / Typescript )如何在Angular 2(Ionic 2 / Angular 2 / Typescript)中的服务中注入服务时传递依赖项参数
【发布时间】:2016-06-13 14:17:58
【问题描述】:

我正在制作一个示例应用程序,以在 typescript 中连接到 ionic 2 中的 websocket 服务器。链接到repo

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

我正在使用angular2-websocket 来创建连接。

参考资料:

  1. 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

我收到一个错误“无法解析 '$WebSocket'(String, Array, ?) 的所有参数。确保所有参数都使用 Inject 进行修饰或具有有效的类型注释并且 '$WebSocket' 已被修饰可注射。”

代码: 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]);

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]);

【问题讨论】:

    标签: typescript ionic-framework angular ionic2 angular2-services


    【解决方案1】:

    我的问题的解决方案是使用 @App() 注释的(特定于离子)提供程序字段而不是使用引导程序

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

    例如。

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

    工作样本可以在here找到

    【讨论】:

    • @App() 是 Ionic 特定的注释?
    • @Gunter 是的,它特定于 Ionic。可以从 'ionic-framework/ionic' 导入;
    • 这意味着你不需要在 Ionic2 应用程序中引导 @tymSpy?
    • App 是一个 Ionic 装饰器,用于引导应用程序。它可以传递许多参数,作为应用程序的全局配置变量。 @App 类似于 Angular 的 @Component,它可以接受具有内联模板的 template 属性,或指向外部 html 模板的 templateUrl 属性。
    【解决方案2】:

    显然$WebSocket 需要stringarray 和另一个使$WebSocket 不可注入的参数,至少不能直接注入。

    作为一种解决方法,您可以使用

    import {Injectable, Component, Inject, provide} from 'angular2/core';
    
    bootstrap(ConnectionService, 
        [provide( $WebSocket, { useValue: new $WebSocket("ws://echo.websocket.org") })]);
    

    如果字符串文字不是您想要的,还有其他选项,例如工厂。

    【讨论】:

    • @tymspy,感谢修复!我自己只使用 Dart,经常忘记语法差异。
    【解决方案3】:

    据我所知,您应该通过注入提供$WebSocket,因为您自己为您的服务实例化了它。我会从bootstrap 中删除它并在构造函数中实例化它,而不是从构造函数参数中提供它:

    @Injectable()
    export class ConnectionService {
      private _status: number;
      private connection: $WebSocket;
    
      constructor() {
        this.connection = new $WebSocket("ws://echo.websocket.org");
      }
    }
    

    也就是说,一般来说,如果你想在 Angular 中利用依赖注入,你必须让 Angular2 自己实例化你想在构造函数中注入的东西。

    在您的情况下,Angular2 会尝试实例化您的 $WebSocket 服务。我认为它的构造函数接受三个参数,当 Angular2 尝试在实例化级别解析这些参数时。最后一个解决不了。能否提供$WebService构造函数的参数?

    【讨论】:

    • url:字符串,协议?:数组,配置?:WebSocketConfig。最后两个是可选的。
    • 事实上 WebSocketConfig 是 Angular2 无法解决的。如果你想在依赖注入中使用$WebSocket,你需要为它设置一个提供者:provide(WebSocketConfig, useValue: new WebSocketConfig(...) 但我认为你会遇到字符串参数的问题......
    • 感谢您的提示。我使用 App decorators 注释的提供程序而不是引导程序,提供($WebSocket, useValue: new WebSocket("ws://echo.websocket.org") 解决了我的问题
    猜你喜欢
    • 1970-01-01
    • 2016-08-07
    • 2016-04-18
    • 2017-08-24
    • 2016-07-19
    • 2015-09-05
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    相关资源
    最近更新 更多