你能只能通过引导程序中注入服务的服务? [英] Can you only inject services into services through bootstrap?

查看:115
本文介绍了你能只能通过引导程序中注入服务的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图线了,使用HTTP服务基本Angular2应用程序。 (大多数我见过的教程由具有做到这一点组件消耗 HTTP 服务,这似乎是错误的,除非薄控制器的基本理念已经改变 - 但是这是一个不同的问题)

I am trying to wire up a basic Angular2 app that uses the Http service. (Most of the tutorials I've seen do this by having a Component consume the Http service, which seems wrong unless the basic philosophy of thin controllers has changed – but that's a different question.)

我想创建一个使用角的 HTTP 服务的服务。但我无法弄清楚如何注入除此之外的 HTTP 服务:

I would like to create a service that uses Angular's Http service. But I can't figure out how to inject the Http service other than this:

boot.ts:

import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from './app.component';
import {HTTP_PROVIDERS } from 'angular2/http';

bootstrap(AppComponent, [HTTP_PROVIDERS]);

myService.ts:

myService.ts:

import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';

@Injectable()
export class aService{
    constructor(http:Http){
    }
    /** do some stuff *//
}

这工作,但它似乎非常错误的,要求服务的用户知道服务的依赖,并要求其注入到引导过程。好像应该有办法直接用手一提供商阵列服务同样的方式,你可以一个组成部分,但我不能找到它。我只是失去了一些东西?

This works, but it seem very wrong to require the user of the service to know the service's dependencies and be required to inject them into the bootstrap process. It seems like there should be a way to directly hand a providers array to a service the same way you can a component, but I can't find it. Am I just missing something?

推荐答案

更新

这样,如果父注入器可提供 OtherService 这一个用于实现,否则,使用 OtherServiceImpl (默认情况下)。

This way if a parent injector provides an implementation for OtherService this one is used, otherwise OtherServiceImpl is used (default).

@Injectable()
class SomeService {
  OtherService _other;

  SomeService(Injector injector) {
    _other = injector.getOptional(OtherService);
    if (_other == null) {
      _other = injector.resolveAndCreateChild([
        provide(OtherService, useClass: OtherServiceImpl)
      ]).get(OtherService);
    }
    _other.doSomething();
  }
}

如果你提供了一个又一个像

If you provide another one like

bootstrap(AppElement, [
  provide(OtherService, useClass: OtherServiceImpl2)
]);

OtherServiceImpl2 被使用。

另请参阅 https://github.com/angular/angular/issues/5622

原始

您可以只作 HTTP 可选服务(使用 @optional()注释),如果没有一个提供只需要创建一个构造函数中的实例与新的HTTP()
这样,用户不会需要,以了解服务的依赖关系,但能够在必要时通过替代的实现(例如,用于测试)。
如果创建服务里面的dependeny需要DI本身,你可以注入注射器,并用它来获得依赖关系。
另请参阅可选的依赖关系的http://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html

You could just make the http service optional (using the @Optional() annotation) and if none is provided just create an instance inside the constructor with new Http(). This way the user doesn't need to know about the services dependencies, but is able to pass alternative implementations if necessary (for example for testing). If creating the dependeny inside the service requires DI itself, you can inject an injector and use it to get dependencies. See also optional dependencies in http://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html

什么也可以工作(没试过自己还)仅仅是创建一个子注入,并指示它跳过自

What also could work (not tried myself yet) is just to create a child injector and instruct it to skip self

SkipSelfMetadata 文档

  class Dependency {
  }

  @Injectable()
  class NeedsDependency {
    dependency;
    constructor(@SkipSelf() dependency:Dependency) {
      this.dependency = dependency;
    }
  }

  var parent = Injector.resolveAndCreate([Dependency]);
  var child = parent.resolveAndCreateChild([NeedsDependency]);
  expect(child.get(NeedsDependency).dependency instanceof Depedency).toBe(true);

  var inj = Injector.resolveAndCreate([Dependency, NeedsDependency]);
  expect(() => inj.get(NeedsDependency)).toThrowError();

我还不知道这是否仍自我解决,如果父母不能提供所请求的类型。

I don't know yet if this still resolves from "self" if parent can't provide the requested type.

这篇关于你能只能通过引导程序中注入服务的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆