【发布时间】:2019-10-31 14:09:13
【问题描述】:
说,Angular 中的两个组件之间存在关系:
// Parent component: ParentComponent
// Child component: ChildComponent
// in ParentComponent template
<pp-child></pp-child>
------
export class ChildComponent implements OnInit {
constructor(
// wonder if there's something like:
// private parentRef: HostParentRef
) {}
ngOnInit() {
console.log(this.parentRef); // This would print ParentComponent
}
}
我想知道是否有办法在ChildComponent 级别中找出其父组件的type。如果有,在这种情况下我会得到ParentComponent。
任何见解将不胜感激!
编辑: 我不是在谈论访问父母的财产或在两者之间进行交流。我只是想知道运行时组件的父组件的type。
【问题讨论】:
-
身份是什么意思?如果您想将任何数据从父组件共享给子组件,您可以将其作为道具传递。如果您能描述您当前的问题是什么,将会很有帮助!
-
@RashiqKodakkad 组件类型我想。在上述情况下,
ParentComponent就是这种情况。 -
@DongBinKim 请示范
-
@shaktimaan 我有,但我没有看到任何与我的问题有关的线索。
标签: angular