在另一个组件构造函数中传递 Angular 组件

Passing Angular component in another component constructor

我是 Angular v2+ 的新手,我看到了一段在构造函数中获取组件对象的代码,如下所示:

  constructor(private host: MapComponent) {
  }

Full code is here。这是正确的方法吗?

您可以注入组件,但要使用 @ViewChild 装饰器。

根据这个architecture-components你不能通过那条路

Angular 组件可以通过多种方式传递数据:

使用 @Input@Output

通过构造函数注入父组件或通过@ViewChild@ViewChildren@ContentChild@ContentChildren注入子组件,直接调用组件的API

使用服务(包括 ngrx 等状态管理库)

使用路由器参数

前两种方法只有在有parent — child关系时才能使用

我们的组件之间 — 其中一个组件是

中的父节点