From Angular documentation : 우리가 Hierarchical Dependency Injectors의 문서의 소스 파일을 열 경우각도에서 하위 구성 요소의 "상단 구성 요소"에서 DI를 가져 오는 방법은 무엇입니까?
Create a "top component" that acts as the root for all of the module's components. Add the custom HttpBackend provider to the top component's providers list rather than the module's providers. Recall that Angular creates a child injector for each component instance and populates the injector with the component's own providers.
When a child of this component asks for the HttpBackend service, Angular provides the local HttpBackend service, not the version provided in the application root injector. Child components make proper http requests no matter what other modules do to HttpBackend.
Be sure to create module components as children of this module's top component.
, 우리가 ACarComponent
, BCarComponent
및 CCarComponent
이 CarService
, CarService2
및 CarService3
의 세 가지 인스턴스를 생성 것을 볼 수 있습니다.
그러나 모듈 수준에서 CarService
을 제공하는 경우 하위 구성 요소의 부모 DI에서 CarService
을 어떻게 얻을 수 있습니까? BCarComponent
을 던졌습니다.하지만 부모 컴포넌트를 하위 컴포넌트에서 가져올 수있는 방법은 무엇입니까 - 무슨 뜻입니까? 당신의 유스 케이스는 무엇입니까? 구성 요소 또는 모듈에서 공급자를 정의합니까? –
@ AngularInDepth.com, 견적에서 볼 수 있듯이 "top component"에 대한 공급자를 정의했으며 내부 자식 구성 요소에 대해이 지점에서 DI를 가져 오려고합니다. – ktretyak
모든 중간 구성 요소 인젝터를 건너 뛰고 싶습니까? Suren이 권장하는대로 모듈 수준의 공급자? –