Javascript angular 2何时调用组件上的onDestroy?
我不知道angular什么时候决定调用onDestory事件 当我使用Javascript angular 2何时调用组件上的onDestroy?,javascript,angular,Javascript,Angular,我不知道angular什么时候决定调用onDestory事件 当我使用*ngIF指令切换组件时,不会调用onDestory,并且组件的状态会保持为使用组件的相同实例 有人能详细说明什么时候角(2)破坏组件吗?以及如何在使用*ngIf切换时实现组件的更新实例?Angular中的大多数DOM操作都是使用。特别是,ngIf的这种机制: private _updateView() { ... this._viewContainer.clear(); ... this.
*ngIF
指令切换组件时,不会调用onDestory,并且组件的状态会保持为使用组件的相同实例
有人能详细说明什么时候角(2)破坏组件吗?以及如何在使用
*ngIf
切换时实现组件的更新实例?Angular中的大多数DOM操作都是使用。特别是,ngIf的这种机制:
private _updateView() {
...
this._viewContainer.clear();
...
this._thenViewRef =
this._viewContainer.createEmbeddedView(this._thenTemplateRef, this._context);
}
}
以及:
@指令({selector:'router outlet',exportAs:'outlet'})
导出类RouterOutlet实现OnDestroy,OnInit{
构造函数(…,私有位置:ViewContainerRef,…)
分离():ComponentRef
每当您更改路由或甚至关闭会话时,都会调用组件onDestroy,如果您希望切换组件的新实例,则可能需要查看事件发射器,并将组件嵌套在一起,以便切换新的组件components@RahulSingh,你在什么地方有文件参考资料吗?很难找到关于这方面的参考资料。我很抱歉有点惊讶的是,你的答案只得到了这么少的赞成票,这是一件非常重要的事情
@Directive({selector: 'router-outlet', exportAs: 'outlet'})
export class RouterOutlet implements OnDestroy, OnInit {
constructor(..., private location: ViewContainerRef, ...)
detach(): ComponentRef<any> {
...
this.location.detach();
...
}
attach(ref: ComponentRef<any>, activatedRoute: ActivatedRoute) {
...
this.location.insert(ref.hostView);
}