- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望能够从内部销毁我的组件。 (不是来自父级,因为它是在多个区域动态创建的)。
我从 angular 的 api 中读到他们有一个 ComponentRef 对象。我试过将它包含在构造函数中,但它说它需要一个参数,我不确定要传递给它什么。
链接:https://angular.io/api/core/ComponentRef
如何在我的组件中使用 ComponentRef 来销毁它?
import { Component, ComponentRef, OnInit } '@angular/core';
export class MyComponent implements OnInit {
constructor(private ref: ComponentRef) {}
ngOnInit() {
this.ref.destroy()
}
}
最佳答案
当您从另一个组件(父组件)实例化组件时,这很有用。
您可以尝试从组件对象中调用 ngOnDestroy() this.ngOnDestroy()
.
如果您正在使用的 Angular 版本不允许您使用,并且由于您想从同一个组件中销毁它,您需要要求父级使用输出和/或发射器来完成它。
关于angular - 如何使用 ComponentRef 从内部销毁我的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45757822/
我希望能够从内部销毁我的组件。 (不是来自父级,因为它是在多个区域动态创建的)。 我从 angular 的 api 中读到他们有一个 ComponentRef 对象。我试过将它包含在构造函数中,但它说
如果我们有一个动态(动态创建 - 非声明式)ComponentRef 实例,并且我们在该实例上调用 destroy(),是否会取消对 的任何订阅已订阅的 >EventEmitter 实例。 例如,如果
所以,我正在尝试使用 heat 为我的项目中的文件生成片段。查看我过去生成的文件以及网络上的各种 WiX 文章,我看到我有目录/组件的片段,然后是使用 ComponentRef 定义 Componen
已经回答了如何从 Angular 2 组件获取 DOM 元素:ComponentRef.location.nativeElement(ComponentRef.location 给出了直接访问 DOM
我正在制作一个弹出组件,我想在其他几个组件中使用它,因此我制作了一个 popup.service ,使该组件能够通过其他组件内的 *ngIf 加载。这给我带来了一个问题,因为 PopupCompone
我尝试在使用 ViewContainerRef 和 ComponentFactoryResolver 创建组件后立即将 CSS 类添加到该组件。我希望能够根据已添加到 myViewContainerR
我遇到 typescript 错误 error TS2339: Property 'close' does not exist on type '{}' 我遇到问题的代码:- home.directi
我正在以 Angular 2 构建模态服务。我解决了大部分问题,但我无法以良好的 Angular 方式将模态组件放置在 body 元素中。我使用 DynamicComponentLoader.load
无法删除 ionic-2 中的动态组件。 typescript 编译时说异常 “Generic type 'ComponentRef' requires 1 type argument(s)”. 此外
我已经按照angular2的官方指南学习了angular2。当我使用angular2-alpha28时,一切正常!将 angular2 更改为 alpha36 时,它不起作用!它显示以下错误: EXC
我是一名优秀的程序员,十分优秀!