Javascript 无法读取属性';nativeElement';使用ngif设置null

Javascript 无法读取属性';nativeElement';使用ngif设置null,javascript,angular,typescript,karma-jasmine,istanbul,Javascript,Angular,Typescript,Karma Jasmine,Istanbul,我正在做一些单元测试 所以我有这个函数: selectCluster(event: MouseEvent, feature: any) { event.stopPropagation(); this.selectedCluster = {geometry: feature.geometry, properties: feature.properties}; } <ng-template mglClusterPoint let-feature>

我正在做一些单元测试

所以我有这个函数:

selectCluster(event: MouseEvent, feature: any) {
    event.stopPropagation(); 
    this.selectedCluster = {geometry: feature.geometry, properties: feature.properties};
  }
 <ng-template mglClusterPoint let-feature>
        <div class="marker-cluster" (click)="selectCluster($event, feature)">
          <fa-icon [icon]="faVideo" [styles]="{'stroke': 'black', 'color': 'black'}" size="lg" class="pr-2"></fa-icon>
          <fa-icon [icon]="faWifi" [styles]="{'stroke': 'black', 'color': 'black'}" size="lg" class="pr-2"></fa-icon>
        </div>
      </ng-template>
和临时板:


 <mgl-popup *ngIf="selectedCluster" [feature]="selectedCluster">
      
    </mgl-popup>

但我仍然得到这个错误:

Cannot read property 'nativeElement' of null
那么我要改变什么呢

多谢各位

这也使用了以下功能:

selectCluster(event: MouseEvent, feature: any) {
    event.stopPropagation(); 
    this.selectedCluster = {geometry: feature.geometry, properties: feature.properties};
  }
 <ng-template mglClusterPoint let-feature>
        <div class="marker-cluster" (click)="selectCluster($event, feature)">
          <fa-icon [icon]="faVideo" [styles]="{'stroke': 'black', 'color': 'black'}" size="lg" class="pr-2"></fa-icon>
          <fa-icon [icon]="faWifi" [styles]="{'stroke': 'black', 'color': 'black'}" size="lg" class="pr-2"></fa-icon>
        </div>
      </ng-template>

.marker cluster
在您尝试单击它时不在页面上,我看不到您的完整html,但我假设它与
*ngIf=“selectedCluster”
相关

 fit('Should set selectedCluster when clicked', async(() => {    
    // spy on and calling through doesn't actually call the function
    // it makes it so we can determine if the function was called
    // and everytime the function was called, call the actual function
    // and not a null function
    spyOn(component, 'selectCluster').and.callThrough();
    // calling the function will should make selectedCluster true
    component.selectCluster({ stopPropagation: () => null } as MouseEvent, {}); // send your own inputs
    fixture.detectChanges();
    fixture.debugElement.query(By.css('.marker-cluster')).nativeElement.click();

    fixture.whenStable().then(() => {
      expect(component.selectCluster).toHaveBeenCalled();
    });
  }));

嗨,谢谢。我又添加了一些html。但在这种情况下,我仍然得到相同的错误是的,它与state=trueOk有关,您必须理解为什么
标记集群
在该时间点为空。也许你没有把所有的东西都正确地设置好以便粉刷?是的,我明白我的想法。因为您是对的,所以在加载HML时它是空的。因为它来自地图。MapboxOk,在调用第一个
检测更改之前,请确保它是真实的。