简体   繁体   English

如何判断子组件的父类是什么

[英]How to tell what the parent class of a child component is

I have a component that I use as a child to two other components. 我有一个用作其他两个组件的子组件。 These two parent components are different classes and I want to be able to do something based off of which one I'm in. 这两个父组件是不同的类,我希望能够根据自己所处的那个来做某事。

Is there a way to tell this using something like @Host or @Inject and instanceof ? 有没有办法使用@Host@Injectinstanceof来告诉这一点? If not, is there another way? 如果没有,还有其他方法吗?

My recommendation is to not do it this way. 我的建议是不要这样做。 If you need a different behavior on the child component either: 如果在子组件上需要其他行为,请执行以下任一操作:

  • Use an @Input property that the parent must fill in, then the child knows what to do with that value. 使用父项必须填写的@Input属性,子项便知道如何处理该值。

  • Alternatively, use some intermediary, like a state (ie ngrx or akita) or a service that mediates between the components. 或者,使用某种中介,例如状态(即ngrx或秋田)或在组件之间进行中介的服务。

Having children know about the parent the way you propose is possible, but again, not what I would recommend. 让孩子以您的提议方式了解父母是可能的,但同样,我不建议这样做。

For example, imagine you have a calendar button, and you use it in 300 hundred different parents, but 150 want it color blue and 150 want it color red. 例如,假设您有一个日历按钮,并且在300万个不同的父母中使用了它,但是150个希望它为蓝色,而150个希望为红色。

If you go the way of switching on the parents, you would have to add 300 conditions to your child based on the parent. 如果您选择与父母联系,则必须基于父母为孩子增加300个条件。

Instead, if you pass an input property or a service property is shared, then you don't have to add but generic code to deal with that set of values, which in the red blue is 2. 相反,如果您传递了输入属性或服务属性被共享,那么您不必添加通用代码即可处理该组值,红色蓝色为2。

We can use redux thinking here. 我们可以在这里使用redux思维。 Set a state saved in a service , aka the store in redux . 设置一个state保存在service ,又名storeredux Every time one of the two parent component is used, update the state . 每次使用两个父组件之一时,请更新state The child component will know which parent component is being used by reading the state . 子组件将通过读取state来知道正在使用哪个父组件。

Assuming that the parent components don't have any hierarchical relationship or are siblings, the simplest way of doing this and with type-safety is by creating an enum for your parent type: 假设父组件没有任何层次关系或同级,那么做到这一点并具有类型安全性的最简单方法是为父类型创建一个enum

export enum Parent {
  ParentA = 'Parent A',
  ParentB = 'Parent B'
}

Then you can create an @Input property in your ChildComponent , something like this: 然后,你可以创建一个@Input你的财产ChildComponent ,是这样的:

import { Component, Input } from '@angular/core';

import { Parent } from './parent-type.enum';

@Component({
  selector: 'child',
  template: `<h1>My Parent IS {{ parent }}</h1>`,
  styles: [`h1 { font-family: Lato; }`]
})
export class ChildComponent  {
  @Input() parent: Parent;
}

And use it like this in your parents: 并在您的父母中这样使用它:

For Parent Component 1: 对于父组件1:

import { Component, Input } from '@angular/core';
import { Parent } from './parent-type.enum';

@Component({
  selector: 'parent-a',
  template: `<child [parent]="parent"></child>`,
  styles: [`h1 { font-family: Lato; }`]
})
export class ParentAComponent  {
  parent = Parent.ParentA;
}

For Parent Component 2 对于父组件2

import { Component, Input } from '@angular/core';
import { Parent } from './parent-type.enum';

@Component({
  selector: 'parent-b',
  template: `<child [parent]="parent"></child>`,
  styles: [`h1 { font-family: Lato; }`]
})
export class ParentBComponent  {
  parent = Parent.ParentB;
}

Here's a Sample StackBlitz for your ref. 这是供您参考的示例StackBlitz

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

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