【问题标题】:How to get child injectors(services) list from a component in angular 4?如何从角度 4 的组件中获取子注入器(服务)列表?
【发布时间】:2019-03-04 11:29:07
【问题描述】:

我有一个称为父组件的组件,并且有子组件 1 和子组件 2。两个子组件都扩展了父组件。 我将服务 A 和服务 B 注入到子组件 1 我将服务 C 和服务 D 注入到子组件 2。 这些子组件是延迟加载的组件。

现在,我需要父组件提供的那些服务。 如何从父组件获取服务列表,无论在该层次结构下注入什么,我都应该获取该子组件注入器下的所有服务。

请告诉我获得这些服务的方法。

【问题讨论】:

  • 你想在哪里访问服务列表?
  • 在另一个组件中,主要是为了显示来自这些服务的视图的当前变化。

标签: javascript angular dependency-injection


【解决方案1】:

我认为你可以在this 上添加服务,因为父母和孩子 共享相同的 this 对象,您可以访问父级中的子服务。

class Parent {
    someMethod() {
       this.serviceList // there you will get all the dependencies of the Child.
   }
}

class Child extends Parent {
    serviceList: Array<any> = []
    constructor(inj Injector) {
           this.serviceList.push ( inj.get(<Dependency1>) );
           this.serviceList.push ( inj.get(<Dependency2>) );
     }
}

【讨论】:

    猜你喜欢
    • 2018-11-23
    • 2019-03-20
    • 2018-03-28
    • 1970-01-01
    • 2021-05-20
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    相关资源
    最近更新 更多