简体   繁体   English

当我们在Angular 2中使用提供程序时?

[英]When we use providers in Angular 2?

Some of examples From angular 2 documents they used for http also 来自角度2文档的一些示例,它们也用于http

import { HTTP_PROVIDERS }    from '@angular/http';
import { HeroService }       from './hero.service';
@Component({
  selector: 'my-toh',
  template: `
  <hero-list></hero-list>
  `,
  directives: [HeroListComponent],
  providers:  [
    HTTP_PROVIDERS,
    HeroService,
  ]
})

Providers need to be used to provide a way to create instances to inject. 需要使用提供程序来提供一种创建要注入的实例的方法。 For example, if you want to inject an Http instance you need to have define the HTTP_PROVIDERS (that contains the provider for the Http type. 例如,如果要注入Http实例,则需要定义HTTP_PROVIDERS (其中包含Http类型的提供程序)。

An important thing to understand is that Angular2 supports hierarchical injectors for dependency injector. 要了解的重要一点是Angular2支持分层注入器作为依赖注入器。 I mean an injector is associated with each component and the current injector is a child injector of the injector of the parent component. 我的意思是说,注射器与每个组件相关联,当前注射器是父组件的注射器的子注射器。

This question could interest you: 这个问题可能会让您感兴趣:

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

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