加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 从CDK Overlay Portal获取对组件的引用

发布时间:2020-12-17 17:52:13 所属栏目:安全 来源:网络整理
导读:我正在使用服务来使用组件门户实例化Angular Material CDK Overlays. 创建门户并将其附加到覆盖后,是否有任何方法可以访问门户创建的组件的组件引用?我希望能够从外部收听该组件的事件.例如: const portal = new ComponentPortal(MyCoolComponent,/* ...et
我正在使用服务来使用组件门户实例化Angular Material CDK Overlays.

创建门户并将其附加到覆盖后,是否有任何方法可以访问门户创建的组件的组件引用?我希望能够从外部收听该组件的事件.例如:

const portal = new ComponentPortal(MyCoolComponent,/* ...etc */);
this.overlay.attach(portal);
// I'd like to be able to do something like...
// portal.MyCoolComponent.someEventEmitter.subscribe();

我已经搜索了文档和来源,找不到办法做到这一点.我可能不得不求助于从服务中注入一个非常混乱的组件回调.

有谁知道如何做到这一点?

解决方法

OverlayRef.attach方法返回ComponentRef. ComponentRef有一个属性实例,它是组件的一个实例. ComponentRef可以是通用的,因此您知道内部组件的类型.

OverlayRef source code中的第60行

attach<T>(portal: ComponentPortal<T>): ComponentRef<T>;

所以你可以在你的代码中做到这一点

const portal = new ComponentPortal(MyCoolComponent,...etc);
const compRef: ComponentRef<MyCoolComponent> = this.overlay.attach(portal);

compRef.instance.someEventEmitter.subscribe();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读