从容器 Ref 中包含的组件调用父级函数



我有一个组件1,其中我使用viewContainerRef包含一个组件2。组件 1 中定义了函数closeSideBar(),我想从组件 2 调用它。 我已经通过在此处搜索尝试了事件发射器和其他各种方法,但没有任何效果。 我怎样才能做到这一点?

您必须在组件 2 中导入 'viewChild' 和 'component1'。

import { ViewChild } from "@angular/core";

在我的情况下,城市组件是组件 1。

import { CityComponent } from "../city/city.component";

在类声明中的行下方插入

@ViewChild(CityComponent) cityComp;

其中 cityComp 是别名。

您可以访问或更改子组件的属性。 就像。

this.cityComp.city = -1; 
this.cityComp.citiesddl = [];

最新更新