Javascript 如何处理render2在Angular中抛出的错误?
我正在处理这个问题,想不出一种方法来处理渲染器2引发的错误。选择RootElement。 这就是我的函数的外观:Javascript 如何处理render2在Angular中抛出的错误?,javascript,angular,typescript,angular-renderer2,Javascript,Angular,Typescript,Angular Renderer2,我正在处理这个问题,想不出一种方法来处理渲染器2引发的错误。选择RootElement。 这就是我的函数的外观: private doSomeAnimation(){ const modalWrapper = this.renderer2.selectRootElement('.modal-wrapper', true);//It fails here // finally do something with modalWrapper this.renderer2.addClass(m
private doSomeAnimation(){
const modalWrapper = this.renderer2.selectRootElement('.modal-wrapper', true);//It fails here
// finally do something with modalWrapper
this.renderer2.addClass(modalWrapper, 'fade-out');
}
如果打开控制台,我会看到以下错误:
global-angular-error-handler.service.ts:49 Error: The selector ".modal-wrapper" did not match any elements
at DefaultDomRenderer2.selectRootElement (dom_renderer.ts:146)
at BaseAnimationRenderer.selectRootElement (animation_renderer.ts:156)
at DebugRenderer2.selectRootElement (services.ts:762)
at ModalComponent.animateClosing (modal.component.ts:39)
....
我还查看了Render2的文档,selectRootElement如下所示
selectRootElement(selectOrNode: string|any):any{
let el: any = typeof selectOrNode === 'string'? document.querySelector(selectOrNode) : selectOrNode;
if(!el){
throw new Error(`The selector "${selectorOrNode}" did not match any elements`);
}
el.textContent = '';
return el;
}
正如您在函数的第1行中看到的,我的代码失败了,因此我的问题是如果没有任何匹配,如何在控制台中不显示任何错误。我的意思是如何在控制台中不显示此消息
“选择器”.modal wrapper“与任何元素都不匹配”
有人能帮我或给我指出正确的方向吗?提前多谢 您可以将呼叫包装为try/catch
let modalWrapper;
try {
modalWrapper = this.renderer2.selectRootElement('.modal-wrapper', true);
} catch (e) {
// do what you need to do with the exception here.
return;
}
您可以将
selectRootElement
调用包装成一个try catch。@AlbertoRivera您可以发布您的解决方案吗?谢谢