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

时间:2019-07-02 07:01:05

标签: angular angular7

我是Angular v2 +的新手,我已经看到了一个代码,该代码在构造函数中获取了这样的组件对象:

  constructor(private host: MapComponent) {
  }

Full code is here。这是真的吗?

2 个答案:

答案 0 :(得分:0)

根据此architecture-components,您不能通过这种方式

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

使用@Input@Output

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

使用服务(涵盖了诸如ngrx之类的状态管理库)

使用路由器参数

只有当我们有父子关系时才可以使用前两种方法

我们的组件之间-组件之一是

中的父节点

答案 1 :(得分:-1)

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