angular - TypeError:无法读取未定义的属性“elementRef”
问题描述
我试图运行单元测试用例,但它抛出错误消息TypeError: Cannot read property 'elementRef' of undefined
list.component.ts
const elementHeader= this.listTable['elementRef'].nativeElement.querySelectorAll('.adf-datatable-header > .adf-datatable-row')[0];
elementHeader.classList.add("testCheck");
let rowsList = this.listTable.rows;
if(rowsList){
for(let i=0;i<=rowsList.length;i++){
if(i==0){
const elementBody= this.listTable['elementRef'].nativeElement.querySelectorAll('.adf-datatable-body > .adf-datatable-row')[0];
elementBody.classList.add("testCheck");
}
const elementBody= this.listTable['elementRef'].nativeElement.querySelectorAll('.adf-datatable-body > .adf-datatable-row')[i];
if( rowsList[i].dtPurged && rowsList[i].dtPurged != null ){
elementBody.classList.add("testCheck");
}
}
list.component.spec.ts
import { ElementRef, Injectable } from '@angular/core';
let h1: HTMLElement;
let elRef: ElementRef;
beforeEach(() => {
h1 = component.listTable['elementRef'].nativeElement.querySelector('.adf-datatable-body > .adf-datatable-row');
});
请帮我解决这个问题
解决方案
在 h1 之前定义 listTable:
beforeEach(() => {
component.listTable = // Define here
h1 = component.listTable['elementRef'].nativeElement.querySelector('.adf-datatable-body > .adf-datatable-row');
});
推荐阅读
- c# - 函数使用初始化的 label.content 而不是我设置的
- c# - 如何从控制器获取身份页面 URL?
- express - 快速路由器 - 路由器加载部分路径?
- java - 具有迭代深化搜索和 5 秒 alpha-beta 修剪停止时间的 minimax
- javascript - google.script.run 没有在由 angularjs 支持的 UI html 中被调用
- python - 如果变量 == 'String' : doSomething()?
- asp.net - 在 IIS 服务器上将 .NET Framework 升级到 4.7.2 会改变网站在 IE 上的显示方式
- mercurial - 如何在不留下悬空头或创建新分支/修订的情况下恢复到 Mercurial 中的先前提交?
- javascript - 在多个单选按钮中选择了哪个选项
- c# - 将值放入 DataTable 的项数组中