处置Angular 2应用或组件 [英] Dispose Angular 2 App or Component
问题描述
反正有处置angular2应用程序或组件的方法吗?我正在混合使用ASP.NET MVC和Angular 2应用程序,其中ASP.NET MVC加载了包括Angular 2 App在内的部分视图.现在,我需要为每个局部视图提供一个关闭按钮,以将自身从DOM中删除,并在视图内部放置加载的Angle 2,以避免内存泄漏.无论如何,有没有要杀死Angular 2应用程序?
Is there anyway to dispose angular2 app or component? I'm doing a mixed ASP.NET MVC and Angular 2 app which ASP.NET MVC loads partial views including Angular 2 App. Now I need a close button for each partial view to remove itself from the DOM and dispose loaded angular 2 inside the view to avoid memory leak. Is there anyway to kill angular 2 app ?
推荐答案
更新
.dispose()
是destroy()
(自beta.16起)
原始
自举组件时,将获得根组件(AppComponent
)的ComponentRef
.要对其进行处置,请调用其dispose()
方法:
When you bootstrap a component, you get a ComponentRef
of the root component (AppComponent
). To dispose it call its dispose()
method:
bootstrap(AppComponent, [ROUTER_PROVIDERS,
provide(LocationStrategy, {useClass: HashLocationStrategy}])
.then((ref) => {
setTimeout(()=> {
ref.dispose();
}, 3000);
});
.catch(err => console.error(err));
(您可以在某个位置分配ref
变量以供以后重用.这只是为了演示可以处理Angular应用程序.)
(You can assign the ref
variable somewhere for later reuse. This is just to demonstrate that an Angular application can be disposed.)
这篇关于处置Angular 2应用或组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!