首页 > 解决方案 > 角度 5 单元测试 - 出现错误“无法读取 null 的属性 offsetHeight”

问题描述

当我对我的应用程序进行单元测试时,它显示以下错误

TypeError: Cannot read property 'offsetHeight' of null

在我的应用程序中,我使用下面的代码来获取 div 元素的高度

totalHeight= (document.getElementById("rcyc-page-header") as HTMLElement).offsetHeight +
                            (document.querySelector("nav.menu-wrapper.brand-nav-wrapper") as HTMLElement).offsetHeight;

当我打印变量“totalheight”的值时,它显示正确,那么为什么会显示此错误?

标签: angular5karma-jasmine

解决方案


推荐阅读