简体   繁体   English

Angular2:需要在组件中添加提供程序以依赖服务吗?

[英]Angular2: Need to add provider in component for dependency on service?

I'm having some trouble understanding the logic that Angular2 uses when it comes to dependency injection. 我在理解Angular2在依赖注入方面使用的逻辑时遇到了一些麻烦。 This is my situation: 这是我的情况:

app.component.ts: app.component.ts:

import {Component} from 'angular2/core';
import {Demo1Service} from './demo-1.service';

@Component({
    selector: 'app',
    template: `<div>test template</div>`,
    providers: [Demo1Service]
})
class AppComponent {
    constructor(private _demo1Service: Demo1Service) {}
}

export {AppComponent};

demo-1.service.ts 演示1.service.ts

import {Injectable} from 'angular2/core';
import {Data1Store} from './data-1.store';

@Injectable()
class Demo1Service {
    constructor(private _data1Store: Data1Store) {}
};

export {Demo1Service};

data-1.store.ts 数据1.store.ts

import {Injectable} from 'angular2/core';

@Injectable()
class Data1Store {}

export {Data1Store};

http://plnkr.co/edit/4aBNbAxtHbUvVqxPRUbv?p=preview http://plnkr.co/edit/4aBNbAxtHbUvVqxPRUbv?p=preview

If you run this plunker you will see that Angular needs a provider for the Data1Store to be defined in the AppComponent. 如果您运行此plunker,您将看到Angular需要在AppComponent中定义Data1Store的提供程序。 In order to have some separation of concerns, I would prefer that the AppComponent doesn't need to know about the existence of the Data1Store. 为了解决一些问题,我希望AppComponent不需要知道Data1Store的存在。

Is there something I'm here? 我有什么事吗?

In fact, the provider needs to present when executing the Demo1Service service. 实际上,提供程序需要在执行Demo1Service服务时显示。 Because of hierarchical injectors, it's not mandatory to have the corresponding provider defined at the component level. 由于分层注入器,在组件级别定义相应的提供程序并不是必需的。

This can also be done for the whole application or within a parent injector. 这也可以针对整个应用程序或在父注射器内完成。 Defining this provider when bootstrapping your application seems to be what you need: 在引导应用程序时定义此提供程序似乎是您所需要的:

bootstrap(AppComponent, [ Demo1Service, Data1Store ]);

This way you don't need to define your service providers within the providers attribute of your component 这样,您无需在组件的providers属性中定义服务提供providers

Here is an overview of all these elements and there relations: 以下是所有这些元素及其关系的概述:

Application
     |
AppComponent  --- Demo1Service --- Data1Store

In such application, we have two injectors: 在这种应用中,我们有两个注射器:

  • The application injector that can be configured using the second parameter of the bootstrap function 可以使用bootstrap函数的第二个参数配置的应用程序注入器
  • The AppComponent injector that can be configured using the providers attribute of this component. 可以使用此组件的providers属性配置的AppComponent注入器。 It can "see" elements defined in the application injector. 它可以“看到”应用程序注入器中定义的元素。 This means if a provider isn't found in this provider, it will be automatically look for into this parent injector. 这意味着如果在此提供程序中找不到提供程序,它将自动查找此父注入程序。 If not found in the latter, a "provider not found" error will be thrown. 如果在后者中找不到,则会抛出“找不到提供者”错误。 This question could give you more details about how hierarchical injectors of Angular work: 这个问题可以为您提供有关Angular分层注入器如何工作的更多详细信息:

  • What's the best way to inject one service into another in angular 2 (Beta)? 在角度2(Beta)中将一项服务注入另一项服务的最佳方法是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM