angular – elementRef.nativeElement.remove()对浏览器有任何负
发布时间:2020-12-17 10:25:07 所属栏目:安全 来源:网络整理
导读:我正在设置超时以销毁Angular 2上可能在调用超时之前销毁的组件.超时以任一方式调用,它在组件的本机元素上执行.remove()(即使它不再在dom中). 如果元素被销毁并且超时执行以删除已经被破坏的组件,那么是否存在任何不可见的负面影响? export class Animation
我正在设置超时以销毁Angular 2上可能在调用超时之前销毁的组件.超时以任一方式调用,它在组件的本机元素上执行.remove()(即使它不再在dom中).
如果元素被销毁并且超时执行以删除已经被破坏的组件,那么是否存在任何不可见的负面影响? export class AnimationCloserComponent { public queryParams$; constructor( private router: Router,private elementRef:ElementRef,private activatedRoute:ActivatedRoute) {} ngOnInit() { /* Will look for routing instructions with QueryParams to route and close this component. These instructions may sometimes not be available.. */ this.queryParams$= this.activatedRoute .queryParamMap .map(params => { let cloSEOutletName = params.get('cloSEOutlet') || null; if (cloSEOutletName != null) { this.router.navigate(['',{ outlets: { [cloSEOutletName]: null }}]); } return params.get('cloSEOutlet') || null; } ); /* This is meant to destroy the component if the router could not route away from it. */ setTimeout(()=>{ this.elementRef.nativeElement.remove(); },1500); } } 我真的想问一下,在我做这个练习之前,这样做是否合适. (已在下面澄清)
如果没有Angular知道它,删除原生DOM元素几乎是不行的. Angular将所有与组件相关的节点(包括子组件)保留在名为
View的抽象中.视图中的节点指向DOM元素.请考虑以下设置:
ComponentA ComponentB 视图层次结构将是这样的: ComponentAView ElementNode('<b-comp>',document.createElement('<b-comp>')) ComponentBView ... ComponentClassNode(new ComponentB()); 如果删除第一个元素< b-comp>来自DOM Angular一无所知.它将继续认为有一个子组件可用. 这可能会导致意外后果,例如Angular报告@ViewChildren中的子组件,同时将其从DOM中删除. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |