简体   繁体   English

将不同的父组件注入子组件

[英]Injecting different parent components into child component

Im trying to solve the following situation:我试图解决以下情况:

I have a child component that will be used in multiple parent components but this child component needs to have access to the parent component (namely its parenComponent.constructor information at runtime).我有一个将在多个父组件中使用的子组件,但该子组件需要访问父组件(即运行时的 parenComponent.constructor 信息)。

In a structure like this在这样的结构中

<BucketComponent>
    <ParentA>
       <ChildComponent>
    </ParentA>
    <ParentB>
       <ChildComponent>
    </ParentB>
<BuccketComponent>

Before this ChildComponent was shared between parents i did the following在这个 ChildComponent 在父母之间共享之前,我做了以下事情

ChildComponentCode子组件代码

component({
  selector: 'childComponent'
})
export class ChildComponent extends BaseClass<model> implements OnInit, OnDestroy {

  constructor(@Inject(ParentComponentClass) parent: Component) {
    super(parent);
  }

and it worked.它奏效了。

But when i tried sharing it as follows :但是当我尝试按如下方式分享它时:

const token = new InjectionToken<Component>('ParentComponentToken')

component({
  selector: 'childComponent',
providers: [
    { provide: token , useClass: ParentComponentClassA},
    { provide : token , useClass : ParentComponentClassB}]
})
export class ChildComponent extends BaseClass<model> implements OnInit, OnDestroy {

  constructor(@Inject(token) parent: Component) {
    super(parent);
  }

In one of the instances of childcomponent i get the correct reference to its parent but not in the other (looks to me like its always referencing one of the two)在 childcomponent 的一个实例中,我得到了对其父级的正确引用,但在另一个实例中却没有(在我看来,它总是引用两者之一)

By the way super(parent);顺便说一下 super(parent); is what uses the parentComponent.constructor information.是什么使用 parentComponent.constructor 信息。

Additionaly i already tried using the @Self() and @Host before the @Inject() but to no avail.另外我已经尝试在@Inject() 之前使用@Self() 和@Host 但无济于事。

Any help into how i can make it so that only the parent where child exists is provided/injected is apreciated.任何关于我如何做到这一点的帮助,以便只提供/注入孩子所在的父项是值得的。

Thank you in advance先感谢您

Check this guide .检查本指南 The idea that parent components provide info about themselves via DI and the child component injects whatever parent component's instance.父组件通过 DI 提供有关自身的信息,子组件注入任何父组件的实例的想法。 The problem here is that parent component can be whatever you can't know in advance so to solve this problem it's better to introduce an abstract base class or interface (and use InjectionToken) and implement it by all parent components to let the child component know what contract to expect.这里的问题是,父组件可以是你事先不知道的任何东西,所以为了解决这个问题,最好引入一个抽象基类或接口(并使用 InjectionToken)并由所有父组件实现它,让子组件知道期待什么合同。

暂无
暂无

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

相关问题 子组件 componentDidMount() 应该调用不同的方法取决于父组件 - Child components componentDidMount() should call different methods depends on parent component 如何将不同的 props 传递给父组件中的不同子组件? - How do you pass different props to different child components in a parent component? 在Angular的父组件中创建子组件 - Create child components in the parent component in Angular 在父组件中反映(递归)的子组件中的计算 - Calculation in child components that reflect(Recursively) in parent component 反应:我们可以将 2 forms 从 2 个不同的子组件传递给父组件,并使用父组件内的按钮提交它们吗? - React: Can we pass 2 forms from 2 different child components to a parent, and submit them with a button which is inside the parent component? 使用 styled-components 从父组件设置子组件的样式 - Style child component from parent component using styled-components 在功能组件中访问父组件内的子组件 ref - Access child components ref inside parent component in functional component 在样式化组件中的子组件的输入焦点上设置父组件 - Style parent component on input focus of child component in styled-components React - 父组件将子组件呈现给不同的元素/节点 - React - parent components render child components to different elements/nodes 角度2-子路径的父组件与父组件不同 - Angular 2 - Child route have a different parent component to parent component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM